2

I'm trying to merge some jsons. I tried these answers but I think they don't fit my needs.

I got 2(or more) of these jsons

{
    item: {
        icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
        icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
        id: 2,
        type: "Default",
        typeIcon: "http://www.runescape.com/img/categories/Default",
        name: "Cannonball",
        description: "Ammo for the Dwarf Cannon.",
        current: {
            trend: "neutral",
            price: 208
        },
        today: {
            trend: "positive",
            price: "+8"
        },
        members: "true",
        day30: {
            trend: "positive",
            change: "+8.0%"
        },
        day90: {
            trend: "negative",
            change: "-4.0%"
        },
        day180: {
            trend: "positive",
            change: "+9.0%"
        }
    }
}

How do I add them to something like this?

{
    items: [
        {
            icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
            icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
            id: 2,
            type: "Default",
            typeIcon: "http://www.runescape.com/img/categories/Default",
            name: "Cannonball",
            description: "Ammo for the Dwarf Cannon.",
            //Same other stuff here
        },
        {
            icon: "http://services.runescape.com/m=itemdb_rs/4922_obj_sprite.gif?id=2",
            icon_large: "http://services.runescape.com/m=itemdb_rs/4922_obj_big.gif?id=2",
            id: 2,
            type: "Default",
            typeIcon: "http://www.runescape.com/img/categories/Default",
            name: "Cannonball",
            description: "Ammo for the Dwarf Cannon.",
            //Same other stuff here     
        }
    ]
}
Community
  • 1
  • 1
Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116

2 Answers2

2

First of all the initial JSON string is not valid, the keys are not enclosed in double quotes, so we need to fix this.

Sample code using the initial string repeated 10 times

<?php

$string = file_get_contents('sample.json');

//var_dump($string);

$data = jsonDecode($string);

for ($i=0;$i<=10;$i++)
    $list['items'][] = $data['item'];

print json_encode($list);

function jsonDecode($string, $assoc=true, $fixNames=true){
  if(strpos($string, '(') === 0){
    $string = substr($string, 1, strlen($string) - 2); // remove outer ( and )
  }
  if($fixNames){
    $string = preg_replace("/(?<!\"|'|\w)([a-zA-Z0-9_]+?)(?!\"|'|\w)\s?:/", "\"$1\":", $string);
  }
  return json_decode($string, $assoc);
}

the json fixing function was take from here https://stackoverflow.com/a/17748840/5043552
the output is as requested

Community
  • 1
  • 1
Alex Andrei
  • 7,315
  • 3
  • 28
  • 42
1

I don't know where each of the json structures are coming from but I assume it will be something that can be iterated through:

$finalArray = array();
foreach ($jsonStructs as $json)
{
    $j = json_decode($json, true);
    $finalArray['items'][] = $j['item'];
}

$finalJson = json_encode( $finalArray);

I take each of the json structures and decode it to a php array. I then take the item value from it and place it into another array that will hold each one.

Finally, I encode the finally array to json which will give you what you asked for.

max_
  • 24,076
  • 39
  • 122
  • 211
dlporter98
  • 1,590
  • 1
  • 12
  • 18