Created
October 20, 2014 09:51
-
-
Save chrisnew/e5286f251319ff94f5d6 to your computer and use it in GitHub Desktop.
Convert a list of the same maps into a CSV file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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