Skip to content

Instantly share code, notes, and snippets.

@chrisnew
Created October 20, 2014 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisnew/e5286f251319ff94f5d6 to your computer and use it in GitHub Desktop.
Save chrisnew/e5286f251319ff94f5d6 to your computer and use it in GitHub Desktop.
Convert a list of the same maps into a CSV file.
#!/usr/bin/php
<?php
// usage: cat some-output.json | ./json2csv.php > some-output.csv
function error($msg, $ret = 1) {
die('ERROR: ' . $msg . PHP_EOL);
exit($ret);
}
$data = json_decode(file_get_contents('php://stdin'), true);
if (!is_array($data) || !count($data) > 0) {
error('invalid input json');
}
fputcsv(STDOUT, array_keys($data[0]));
foreach ($data as $row) {
fputcsv(STDOUT, array_values($row));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment