I am trying to convert a json file into csv format using a php script. The code is as follows:
if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];
$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');
$firstLineKeys = false;
foreach ($array as $line)
{
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
fputcsv($f, array_merge($firstLineKeys, $line));
}
This kind of works, but is only returning the outer variables of the JSON file, and am getting a "Array to string conversion" warning
The JSON data looks like this:
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100355321","value_3":"XXXX","value_4":"12667","value_5":"6"},"stream_type":"COOKIE"}
{"type":"ATTRIBUTED","conversion":{,"value_1":"000000167865321","value_3":"YYYY","value_4":"12668","value_5":"0"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000134535321","value_3":"AAAA","value_4":"12669","value_5":"9"},"stream_type":"COOKIE"}
{"type":"NON_ATTRIBUTED","conversion":{,"value_1":"000000100357651","value_3":"WWWW","value_4":"12670","value_5":"2"},"stream_type":"COOKIE"}
The output I am getting is : type,conversion,stream_type NON_ATTRIBUTED,Array,COOKIE NON_ATTRIBUTED,Array,COOKIE
The output I am expecting is: type,conversion,value_1,value_3,value_4, value_5 ,stream_type NON_ATTRIBUTED,000000100355321, XXXX, 1267, 6, COOKIE ..
ANy help appreciated as this is very new to me
On the “Data” tab, from the “Get & Transform Data” section, select Get Data > From File > From JSON. You will see your computer's standard “Import” window. Here, open the folder where your JSON file is located. Double-click the file to connect it to Excel.
json_decode($json, true); converts JSON objects to associative arrays. So this
{
"type":"NON_ATTRIBUTED",
"conversion":{,
"value_1":"000000100355321",
"value_3":"XXXX",
"value_4":"12667",
"value_5":"6"
},
"stream_type":"COOKIE"
}
Become this:
array(3) {
["type"]=> string(14) "NON_ATTRIBUTED"
["conversion"]=> array(4) {
["value_1"]=> string(15) "000000100355321"
["value_3"]=> string(4) "XXXX"
["value_4"]=> string(5) "12667"
["value_5"]=> string(1) "6"
}
["stream_type"]=> string(6) "COOKIE"
}
As you see there is nested arrays. And you trying to insert all elements of array to your text file (csv is just a simple text file) with this line:
fputcsv($f, array_merge($firstLineKeys, $line));
It works nice when element of array is string. But when the element is array we got the Array to string conversion. So you must to use loop or array_merge on a nested array to prevent this.
I can't clearly understand how your csv must look like, but I hope this fix of your code will help you. If not, write a comment below.
if (empty($argv[1])) die("The json file name or URL is missed\n");
$jsonFilename = $argv[1];
$json = file_get_contents($jsonFilename);
$array = json_decode($json, true);
$f = fopen('output.csv', 'w');
$firstLineKeys = false;
foreach ($array as $line)
{
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($line);
fputcsv($f, $firstLineKeys);
$firstLineKeys = array_flip($firstLineKeys);
}
$line_array = array($line['type']);
foreach ($line['conversion'] as $value)
{
array_push($line_array,$value);
}
array_push($line_array,$line['stream_type']);
fputcsv($f, $line_array);
}
There is also a mistake in your json - unneeded comma: "conversion":{,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With