0

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:

  1. Print out the keys that starts with foo_ in a column
  2. 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();
  ?>

1 Answer 1

0

I have tested the code below using this dummy jsonfile, and it answers perfectly the question I posted.

 <?php
 require_once 'C:/xampp/htdocs/json/pcrov/vendor/autoload.php'; //make sure 
 that this is a correct file path
 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, uhc".PHP_EOL);
 {
     if (preg_match('/^foo_/', $reader->name())){ //To retreive keys that 
 // starts with `foo_`, wildcard is employed here using regular expression 
 // function in php. 
    $companyID = $reader->name();
    $companyID1 = str_ireplace("foo_","",$companyID); // this line is 
 // necessary in order to remove foo_ because what is needed in the output 
 // is everything after `foo_`.

    if ($reader->read("entityName")){
        $entityName = $reader->value();
        $entityName = str_ireplace(","," ",$entityName); //comma need to be 
 // replaced because csv treats comma as a delimiter. if comma not removed, 
 // csv pushes everything after comma to another column
    }

    if ($reader->read("ultimateHoldingCompany")){       
        $uhcName = null;
        $uhc = $reader->value();
        if (empty($uhc)){
        }
        else {
            $uhcName = $uhc[0]['name'];
            $uhcName = str_ireplace(","," ",$uhcName); //comma need to be 
 // replaced because csv treats comma as a delimiter. if comma not removed, 
 // csv pushes everything after comma to another column
        }

    }       

    fputs($fo,  $companyID1.",".$entityName.",".$uhcName.PHP_EOL);
    }   
  }
 $reader->close();
 ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.