How To Convert CSV To JSON Using PHP?


In this tutorial, we learn how to convert CSV to JSON format in PHP. This tutorial is very easy to explain PHP convert CSV to JSON.

we will learn how to convert CSV data or file into a JSON object in PHP.

This tutorial has a purpose to shows you an easy way to convert CSV data or file to JSON object in PHP.

This is a simple and short tutorial on How to CSV to JSON in PHP. we have to make features like fetch data from CSV file and insert that CSV file data into the Mysql database. we have to need to convert how to convert CSV to JSON. The main purpose of this tutorial is to learn how to convert CSV to JSON.

How To Convert CSV To JSON Using PHP? -Step By Step
Index.php
<!DOCTYPE html>
<html>
  	<head>
    	<title>How To Convert CSV To JSON Using PHP - phpcodingstuff.com</title>
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
      <script src="http://code.jquery.com/jquery.js"></script>
    	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  	</head>
  	<body>
  		<div class="container ">
  		
    		<h1 align="center" class="my-5">How To Convert CSV To JSON Using PHP - phpcodingstuff.com</h1>
    		<br />
    		<div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Select CSV File</h3>
          </div>
          <div class="panel-body">
            <?php
            if($error != '')
            {
              echo '<div class="alert alert-danger">'.$error.'</div>';
            }
            ?>
            <form method="post" enctype="multipart/form-data">
              <div class="col-md-6" align="right">Select File</div>
              <div class="col-md-6">
                <input type="file" name="file" />
              </div>
              <br /><br /><br />
              <div class="col-md-12" align="center">
                <input type="submit" name="upload_file" class="btn btn-primary" value="Upload" />
              </div>
            </form>
          </div>
        </div>
    	</div>
    	
  	</body>
</html>

After this, index.php will add this PHP code. Automatic convert CSV to JSON.

<?php

$error = '';

if(isset($_POST["upload_file"]))
{
  if($_FILES['file']['name'])
  {
    $file_array = explode(".", $_FILES['file']['name']);

    $file_name = $file_array[0];

    $extension = end($file_array);

    if($extension == 'csv')
    {
      $column_name = array();

      $final_data = array();

      $file_data = file_get_contents($_FILES['file']['tmp_name']);

      $data_array = array_map("str_getcsv", explode("\n", $file_data));

      $labels = array_shift($data_array);

      foreach($labels as $label)
      {
        $column_name[] = $label;
      }

      $count = count($data_array) - 1;

      for($j = 0; $j < $count; $j++)
      {
        $data = array_combine($column_name, $data_array[$j]);

        $final_data[$j] = $data;
      }

      header('Content-disposition: attachment; filename='.$file_name.'.json');

      header('Content-type: application/json');

      echo json_encode($final_data);

      exit;
    }
    else
    {
      $error = 'Only <b>.csv</b> file allowed';
    }
  }
  else
  {
    $error = 'Please Select CSV File';
  }
}

?>
How To Convert CSV To JSON Using PHP? -Step By Step
Output JSON Data

Like the CSV file that you have selected, it will be converted as an example in JSON. We learn to convert CSV to JSON, see example in JSON below.

[
   {
    "Name": "Ajay kumar",
    "email": "wxyz@abc.com",
    "City": "New delhi",
    "State": "delhi",
    "ZIP/PIN CODE": "110001",
    "Country": "INDIA",
    "Website": "phpcodingstuff.com"
  },
{
    "Name": "Abhaykumar",
    "email": "wxyz@abc.com",
    "City": "New delhi",
    "State": "delhi",
    "ZIP/PIN CODE": "110001",
    "Country": "INDIA",
    "Website": "phpcodingstuff.com"
  }
]

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!