Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSV to JSON with PHP?

Tags:

json

ajax

php

curl

csv

I need to convert a CSV file to JSON on the server using PHP. I am using this script which works:

function csvToJSON($csv) {
    $rows = explode("\n", $csv);

    $i = 0;
    $len = count($rows);
    $json = "{\n" . '    "data" : [';
    foreach ($rows as $row) {
        $cols = explode(',', $row);
        $json .= "\n        {\n";
        $json .= '            "var0" : "' . $cols[0] . "\",\n";
        $json .= '            "var1" : "' . $cols[1] . "\",\n";
        $json .= '            "var2" : "' . $cols[2] . "\",\n";
        $json .= '            "var3" : "' . $cols[3] . "\",\n";
        $json .= '            "var4" : "' . $cols[4] . "\",\n";
        $json .= '            "var5" : "' . $cols[5] . "\",\n";
        $json .= '            "var6" : "' . $cols[6] . "\",\n";
        $json .= '            "var7" : "' . $cols[7] . "\",\n";
        $json .= '            "var8" : "' . $cols[8] . "\",\n";
        $json .= '            "var9" : "' . $cols[9] . "\",\n";
        $json .= '            "var10" : "' . $cols[10] . '"';
        $json .= "\n        }";

        if ($i !== $len - 1) {
            $json .= ',';
        }

        $i++;
    }
    $json .= "\n    ]\n}";

    return $json;
}

$json = csvToJSON($csv);
$json = preg_replace('/[ \n]/', '', $json);

header('Content-Type: text/plain');
header('Cache-Control: no-cache');
echo $json;

The $csv variable is a string resulting from a cURL request which returns the CSV content.

I am sure this is not the most efficient PHP code to do it because I am a beginner developer and my knowledge of PHP is low. Is there a better, more efficient way to convert CSV to JSON using PHP?

Thanks in advance.

Note. I am aware that I am adding whitespace and then removing it, I do this so I can have the option to return "readable" JSON by removing the line $json = preg_replace('/[ \n]/', '', $json); for testing purposes.

Edit. Thanks for your replies, based on them the new code is like this:

function csvToJson($csv) {
    $rows = explode("\n", trim($csv));
    $csvarr = array_map(function ($row) {
        $keys = array('var0','var1','var2','var3','var4','var5','var6','var7','var8','var9','var10');
        return array_combine($keys, str_getcsv($row));
    }, $rows);
    $json = json_encode($csvarr);

    return $json;
}

$json = csvToJson($csv);

header('Content-Type: application/json');
header('Cache-Control: no-cache');
echo $json;
like image 598
VerizonW Avatar asked Jan 27 '11 00:01

VerizonW


People also ask

How do I import a CSV file into PHP?

php $mysqli = new mysqli("localhost", "root", "", "csv-to-mysql"); $csvFilePath = "import-template. csv"; $file = fopen($csvFilePath, "r"); while (($row = fgetcsv($file)) !==

What does json_ encode do?

The json_encode() function is used to encode a value to JSON format.


2 Answers

None of these answers work with multiline cells, because they all assume a row ends with '\n'. The builtin fgetcsv function understands that multiline cells are enclosed in " so it doesn't run into the same problem. The code below instead of relying on '\n' to find each row of a csv lets fgetcsv go row by row and prep our output.

function csv_to_json($file){

    $columns = fgetcsv($file); // first lets get the keys.
    $output = array(); // we will build out an array of arrays here.

    while(!feof($file)){ // until we get to the end of file, we'll pull in a new line
        $line = fgetcsv($file); // gets the next line
        $lineObject = array(); // we build out each line with our $columns keys
        foreach($columns as $key => $value){
            $lineObject[$value] = $line[$key];
        }
        array_push($output, $lineObject);
    }
    return json_encode($output); // encode it as json before sending it back
}
like image 135
ericjbasti Avatar answered Oct 07 '22 00:10

ericjbasti


Well there is the json_encode() function, which you should use rather than building up the JSON output yourself. And there is also a function str_getcsv() for parsing CSV:

$array = array_map("str_getcsv", explode("\n", $csv));
print json_encode($array);

You must however adapt the $array if you want the JSON output to hold named fields.

like image 34
mario Avatar answered Oct 07 '22 00:10

mario