This is a surprisingly easy task, but nobody’s documented the solution yet. So here it is, the code to convert a CSV file to JSON using PHP.

$fileAsArray = Array();

if (($handle = fopen("myfile.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$fileAsArray[] = $data;
}
fclose($handle);
}

die(json_encode($fileAsArray));

Use it wisely. It's especially useful when interpreted with PHP on the command line.


Recent Posts

Post a Comment

Your email is never shared.

*
*