Skip to content
Advertisement

Merging JSON while keeping key layout

I’m trying to merge multiple JSON results that I get from an API into a single one.

The JSON data I’m receiving looks like the following:

{
  "total": 100,
  "offset": 0,
  "articleCollection": [
    {
      "artikelindeling": {
        "artikelgroep": "string",
        "artikelgroep_omschrijving": "string",
        "kernwoord": "string",
        "kernwoord_omschrijving": "string",
        "klantgroep": "string",
        "klantgroep_omschrijving": "string",
        "plaatsbepaling": "string",
        "plaatsbepaling_omschrijving": "string",
        "unieknummerserie": "string"
      },
      "artikelinformatie": {
        "prijsinformatie": {
          "actie": {
            "actie_bruto_inkoopprijs": 0,
            "actie_consumentenadviesprijs": 0,
            "actie_datum_tot_en_met": "2020-02-11T18:19:51.747Z",
            "actie_datum_vanaf": "2020-02-11T18:19:51.747Z"
          },
          "basisprijs_dealer": 0,
          "btw": "string",
          "btw_omschrijving": "string",
          "consumentenadviesprijs": 0
        }
      },
      "dateModified": "2020-02-11T18:19:51.747Z",
      "date_modified": "string",
      "land": "string",
      "taal": "string",
      "valuta": "string"
    }
  ]
}

Here’s the code I’m going with so far:

        $username = $_POST['username'];
        $password = $_POST['password'];
        $leverancier = $_POST['leverancier'];
        $offset = 0;

        $jsonCombined = array();
        $data = call_curl($username, $password, $leverancier, $offset); //First call needed to get total amount
        $jsonArr = json_decode($data, true);

        if ($jsonArr['total'] > 50) { //API limits to 50 results per call, need multiple calls.
            $totalCalls = floor($jsonArr['total'] / 50); //Calculate how many calls are needed.
            foreach($jsonArr as $record) {
                $jsonCombined[] = $record;
            }
        for ($i = 0; $i < $totalCalls; $i++) {
            $offset += 50;
            $data = call_curl($username, $password, $leverancier, $offset);
            $jsonArr = json_decode($data, true);
            foreach($jsonArr as $record) {  
                $jsonCombined[] = $record;
            }
        }       
        }
        else {
            foreach($jsonArr as $record) {
                $jsonCombined[] = $record;
            }
        }

The desired result would be:

Array ( [total] => 100 [offset] => 0 [articleCollection] => Array ( [0] => Array() [1] => Array() [2] => Array()))

However, I’m receiving:

Array ( [0] => 100 [1] => 0 [2] => Array ( [0] => Array() [1] => Array() [2] => Array()))

How would I go about changing this so the keys stay intact and the additional items are added to “articleCollection”?

Advertisement

Answer

As it’s difficult to test properly, this code is as close as I can guess at.

The problem is that when you add the data in with $jsonCombined[], this will just add the data to the end of the array in a numerical sequence. This code instead gets the key along with the content in lines like…

foreach($jsonArr as $key => $record)

and then uses this key to add the data with the key…

$jsonCombined[$key] = $record;

The inner loop which fetches the pages of data, only copies the "articleCollection" elements, this time using $jsonCombined["articleCollection"][] to just keep on adding the data (this bit is the part I’m not sure exactly if this works OK.)

if ($jsonArr['total'] > 50) { //API limits to 50 results per call, need multiple calls.
    $totalCalls = floor($jsonArr['total'] / 50); //Calculate how many calls are needed.
    foreach($jsonArr as $key => $record) {
        $jsonCombined[$key] = $record;
    }
    for ($i = 0; $i < $totalCalls; $i++) {
        $offset += 50;
       $data = call_curl($username, $password, $leverancier, $offset);
        $jsonArr = json_decode($data, true);
        foreach($jsonArr["articleCollection"] as $record) {
            $jsonCombined["articleCollection"][] = $record;
        }
    }
}
else {
    foreach($jsonArr as $key => $record) {
        $jsonCombined[$key] = $record;
    }
}

The last foreach may not be needed as it just copies the data from $jsonArr to $jsonCombined. So you could just assign the one to the other.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement