I am working with a JSON file that is nested and I am employing a JSON pull parser for PHP 7 due to the size of the file. I want to extract data and put them in a csv table as thus:
FOO ID
NAME
UHC
UHCCOMPANY
foo_1403039
bana
nege
foo_1403039
baan
foo_13589
anab
neeg
nege
foo_13590
naab
foo_1403220
suresh ltd
foo_1403223
suresh ltd
But then if you notice in the table above, the 1st and 2nd data (foo_1403039) under the first column titled FOO ID are duplicated, and I do not want that. Indeed, if I delete the 1st data under the first column, and shift the cell of that column up by one step, the table looks perfect. Similarly, the last two data (suresh ltd) under the under the second column titled NAME are also duplicate.
I do not know what is wrong in my code. Any help will be appreciated. Here is my dummy json file, and this is my php code:
<?php
require_once 'C:/xampp/htdocs/vendor/autoload.php';
use \pcrov\JsonReader\JsonReader;
ini_set("max_execution_time", 0);
$reader = new JsonReader();
$reader->open("myjsonfile.json");
$fo = fopen("mycsv.csv", "w" );
fputs($fo, "companyID1, name, ultimateHoldingCompany, uhcCountry".PHP_EOL);
while($reader->read()) {
if (preg_match('/^foo_/', $reader->name())){
$companyID = $reader->name();
$companyID1 = str_ireplace("foo_","",$companyID);
if ($reader->read("entityName")){
$entityName = $reader->value();
$entityName = str_ireplace(","," ",$entityName);
}
if ($reader->read("ultimateHoldingCompany")){
$uhcName = null;
$uhcCountry = null;
$ultimateHoldingCompany = $reader->value();
if (empty($ultimateHoldingCompany)){
}
else {
$uhcCountry = $ultimateHoldingCompany[0]['country'];
$uhcName = $ultimateHoldingCompany[0]['name'];
$uhcName = str_ireplace(","," ",$uhcName);
}
}
fputs($fo, $companyID1.",".$entityName.",".$uhcName.",".$uhcCountry.PHP_EOL);
}
}
$reader->close();