Skip to content

Instantly share code, notes, and snippets.

@burlresearch
Created April 2, 2015 13:35
Show Gist options
  • Save burlresearch/31409cd27295139c78de to your computer and use it in GitHub Desktop.
Save burlresearch/31409cd27295139c78de to your computer and use it in GitHub Desktop.
Convert JSON files each to CSV.
<?php
$AS_ARRAY = true;
$files = glob('*.json');
foreach($files as $fname) {
echo $fname . PHP_EOL;
$json_data = file_get_contents($fname);
$json_obj = json_decode($json_data, $AS_ARRAY);
$fp = fopen(basename($fname, '.json') . '.csv', 'w');
$hdr = true;
foreach($json_obj as $fields) {
if($hdr) fputcsv($fp, array_keys($fields));
$hdr = false;
fputcsv($fp, $fields);
}
fclose($fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment