I am trying to extract data from a large and nested JSON file using PHP JsonReader, and put it in a .csv. My problems are:
- Print out the keys that starts with
foo_in a column - Print out other object values in the nested file
My desired CSV table is this. Somehow, I am not getting desired result, but I know I have an idea. Here is my dummy file.
Here is my code:
<?php
require_once 'C:/xampp/htdocs/json/pcrov/vendor/autoload.php';
use \pcrov\JsonReader\JsonReader;
ini_set("max_execution_time", 0);
$reader = new JsonReader();
$reader->open("jsonfile.json");
$fo = fopen("csvfile.csv", "w" );
fputs($fo, "name, companyID, ultimateHoldingCompany".PHP_EOL);
while ($reader->read(strpos($key, "foo__"))) {
// I want to loop through the key that contains foo_ and print the key name
$companyID = null;
$entityName = null;
$uhc = null;
$companyID = $key
$entityName = $reader->value();
$UltimateHoldingCompany = $reader['ultimateHoldingCompany']['name']-
>value;
fputs($fo,
$entityName.",".$companyID.",".$UltimateHoldingCompany.PHP_EOL);
}
$reader->close();
?>