Created
August 29, 2014 23:42
-
-
Save viable-hartman/1a563389a551d7cf7d23 to your computer and use it in GitHub Desktop.
Convert file containg JSON record lines into CSV
This file contains 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 | |
/* | |
* Call: php json2csv.php SOMEFILE.json > OUT.csv | |
*/ | |
global $sefh; | |
$sefh = fopen('php://stderr','a'); | |
$jsontemplate; | |
$jsontemplate = array(); | |
function startsWith($haystack, $needle) { | |
return $needle === "" || strpos($haystack, $needle) === 0; | |
} | |
function array_unset_recursive(&$array) { | |
foreach ($array as $key => &$value) { | |
if( is_array($value) ) { | |
array_unset_recursive($value); | |
} else { | |
$array[$key] = ""; | |
} | |
} | |
} | |
function array_del_nulls_recursive(&$array) { | |
foreach ($array as $k => &$v) { | |
if( is_array($v) ) { | |
array_del_nulls_recursive($v); | |
} else { | |
if(is_null($v)) { | |
unset($array[$k]); | |
} | |
else if( trim($v) == "" ) { | |
unset($array[$k]); | |
} | |
} | |
} | |
} | |
function createHEAD(&$array, $parent = null) { | |
$row = ""; | |
$head = ""; | |
if( !is_null($parent) ) { | |
$head = $parent.'_'; | |
} | |
foreach ($array as $k => &$v) { | |
if( is_array($v)) { | |
$row .= createHEAD($v, $head.$k); | |
} else { | |
//$row .= '"'.str_replace(array('"', "\n"), array('', " "), $v).'",'; | |
$row .= '"'.str_replace(array('"'), array(''), trim(preg_replace('/\s+/', ' ', $head.$k))).'",'; | |
} | |
} | |
return $row; | |
} | |
function createROW(&$array) { | |
$row = ""; | |
foreach ($array as $k => &$v) { | |
if( is_array($v)) { | |
$row .= createROW($v); | |
} else { | |
//$row .= '"'.str_replace(array('"', "\n"), array('', " "), $v).'",'; | |
$row .= '"'.str_replace(array('"'), array(''), trim(preg_replace('/\s+/', ' ', $v))).'",'; | |
} | |
} | |
return $row; | |
} | |
if (empty($argv[1])) die("The json file name or URL is missed\n"); | |
$jsonFilename = $argv[1]; | |
if (file_exists($jsonFilename) && is_readable ($jsonFilename)) { | |
$fh = fopen($jsonFilename, "r"); | |
// Building JSON Template | |
$i = 0; | |
while (!feof($fh)) { | |
//fwrite($sefh, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ line $i ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); | |
$i++; | |
$line = trim(fgets($fh)); | |
// Json file starts each row with a comma, so remove it | |
if(startsWith($line, ',')) { | |
$line = substr($line, strpos($line, ',') + 1); | |
} | |
if($line != "") { | |
$array = json_decode($line, true); | |
if( !is_array($array) ) { | |
fwrite($sefh, "Line $line could not be decoded!"); | |
} else { | |
array_del_nulls_recursive($array); | |
$jsontemplate = array_replace_recursive($jsontemplate, $array); | |
} | |
} | |
} | |
fclose($fh); | |
array_unset_recursive($jsontemplate); | |
// print_r($jsontemplate); | |
$fh = fopen($jsonFilename, "r"); | |
// Build Uniform CSV Utilizing Template | |
echo(createHEAD($jsontemplate)."\n"); | |
$i = 0; | |
while (!feof($fh)) { | |
//fwrite($sefh, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ line $i ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); | |
$i++; | |
$line = trim(fgets($fh)); | |
// Json file starts each row with a comma, so remove it | |
if(startsWith($line, ',')) { | |
$line = substr($line, strpos($line, ',') + 1); | |
} | |
if($line != "") { | |
$array = json_decode($line, true); | |
if( is_array($array) ) { | |
$temprow = $array; | |
array_del_nulls_recursive($array); | |
$jsonrow = array_replace_recursive($jsontemplate, $array); | |
echo(createROW($jsonrow)."\n"); | |
// fwrite($sefh, "~~~~~~~~ ROW: ".sizeof($jsonrow)." HEADER: ".sizeof($jsonrow['header'])." ADDR: ".sizeof($jsonrow['header']['address'])."~~~~~~~~\n"); | |
} | |
} | |
} | |
fclose($fh); | |
} | |
fclose($sefh); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment