I have multiple JSON files that I would like to parse, edit, and merge into one object, which will ultimately get re-encoded and output as a single JSON.
I currently have a working example of when I parse and edit one object, but I can’t figure out how to do two and get a single result.
Working example:
$source = 'http://www.jakedup.com/_/source1';
$data = json_decode(file_get_contents($source));
// THIS WILL BE THE FINAL OUTPUT OBJECT
$output = new stdClass();
// GET THE ARTICLE DATES AS AN ARRAY
$tracks = get_object_vars($data->tracks);
// LOOPS OVER ARTICLE PUBDATES TO GET THE OUTER OBJECTS
foreach ($tracks as $key=>$val) {
// LOOPS OVER THE INNER OBJECTS IN EACH ARTICLE PUBDATE
foreach ($data->tracks->$key as $object) {
// ADD IT ONTO THE OUTPUT OBJECT WITH "ID" AS PARENT NAME
$output->{$object->id} = $object;
}
}
echo json_encode($output);
Here are my JSON examples. I am providing two examples, but I am looking for a way to merge an infinite number of JSON files dynamically.
JSON Source1:
{
"foo":"bar",
"tracks":{
"1349496000":[
{
"id":"25328",
"is_promo":null,
"is_feature":null,
"listens":"1312",
"downloads":"777"
},
{
"id":"25327",
"is_promo":null,
"is_feature":null,
"listens":"1255",
"downloads":"578"
},
{
"id":"25329",
"is_promo":null,
"is_feature":null,
"listens":"341",
"downloads":"139"
}
],
"1349323200":[
{
"id":"25310",
"is_promo":null,
"is_feature":null,
"listens":"10793",
"downloads":"4953"
}
]
}
}
JSON Source 2:
{
"foo":"bar",
"tracks":{
"1349323200":[
{
"id":"25303",
"is_promo":null,
"is_feature":null,
"listens":"2347",
"downloads":"1499"
}
],
"1349236800":[
{
"id":"25266",
"is_promo":null,
"is_feature":null,
"listens":"24485",
"downloads":"19366"
}
]
}
}
I have been toying around with the idea of grabbing sources in a loop, but I am stuck when it gets to merging the data. All my different efforts have not produced the right data structure.
Current test code:
$source = 'http://www.jakedup.com/_/source';
$pages = 2;
for ($i = 1; $i <= $pages; $i++) {
$sources[$i] = $source.$i;
${'source'.$i} = json_decode(file_get_contents($source.$i));
}
I now have two objects — $source1 & $source2 — but I’m not sure what to do next.
This is the output I am expecting:
{
"25328": {
"id": "25328",
"is_promo": null,
"is_feature": null,
"listens": "1312",
"downloads": "777",
"timestamp": "1349496000"
},
"25327": {
"id": "25327",
"is_promo": null,
"is_feature": null,
"listens": "1255",
"downloads": "578",
"timestamp": "1349496000"
},
"25329": {
"id": "25329",
"is_promo": null,
"is_feature": null,
"listens": "341",
"downloads": "139",
"timestamp": "1349496000"
},
"25310": {
"id": "25310",
"is_promo": null,
"is_feature": null,
"listens": "10793",
"downloads": "4953",
"timestamp": "1349323200"
},
"25303": {
"id": "25303",
"is_promo": null,
"is_feature": null,
"listens": "2347",
"downloads": "1499",
"timestamp": "1349323200"
},
"25266": {
"id": "25266",
"is_promo": null,
"is_feature": null,
"listens": "24485",
"downloads": "19366",
"timestamp": "1349236800"
}
}
Thanks in advance for your help!
This is a little more simply done with a PHP array rather than trying to build the
stdClassobject with numeric properties.json_encode()will automatically produce an object{}as opposed to an array[]if the PHP array has non-consecutive, non-zero based keys. Likewisejson_decode()has an optional second parameter to produce an associative array rather than an object. So I would recommend decoding the files as an array and looping over them, appending eachidonto an output array, which you will ultimatelyjson_encode()again.Your code implies that you have JSON source files like ‘http://www.jakedup.com/_/source1’. You were on the right track, but instead of populating new variable variables, retrieve and decode each file in a loop, and append it directly to the output array.
JSON output: