Parse JSON Data with PHP

I have parsed JSON data numerous times but for some reason cannot find the correct syntax to use when the data is nested. I am trying to parse the “assets” from this JSON but continue to get a invalid argument supplied foreach() regardless of what I try.

   "3435":{
      "name":"COLO-1014-SJ1",
      "nickname":"US-SJC-004",
      "type":"Colocated Server",
      "location":"San Jose:55 S Market",
      "assets":{
         "CPU":[
            {
               "model":"Intel E3 1270"
            }
         ],
         "Hard Drives":[
            {
               "model":"Western Digital 500GB RE4 ABYX SATA"
            },
            {
               "model":"Western Digital 500GB RE4 ABYX SATA"
            },
            {
               "model":"Kingston 240GB SSD"
            }
         ],
         "RAM":[
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            },
            {
               "model":"Super Talent 4GB DDR3 1333 ECC"
            }
         ],

I would expect it to be something along the lines of…

$json = json_decode($jsondata, true);

foreach ($json as $item)
{
    foreach ($item['assets']->RAM as $asset)
    {
        echo $asset->model;
    }

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

It looks like you have forgot to add the surrounding curly braces around the JSON data. If your JSON data is invalid then the json_decode() function will not work correctly.

I have fixed your JSON code below and this now validates and meets the JSON standard.

{
    "3435": {
        "name": "COLO-1014-SJ1",
        "nickname": "US-SJC-004",
        "type": "Colocated Server",
        "location": "San Jose:55 S Market",
        "assets": {
            "CPU": [
                {
                    "model": "Intel E3 1270"
                }
            ],
            "Hard Drives": [
                {
                    "model": "Western Digital 500GB RE4 ABYX SATA"
                },
                {
                    "model": "Western Digital 500GB RE4 ABYX SATA"
                },
                {
                    "model": "Kingston 240GB SSD"
                }
            ],
            "RAM": [
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                },
                {
                    "model": "Super Talent 4GB DDR3 1333 ECC"
                }
            ]
        }
    }
}

If you ever need to check your JSON code you can use a validator such as http://jsonlint.com/

Secondly, your PHP code is also wrong:

$json = json_decode($jsondata, true);

foreach ($json as $item)
{
    foreach ($item->assets->RAM as $asset)
    {
        echo $asset->model;
    }
}

You have been trying to access the returned object as an array which will also cause issues with the foreach loop.

Solution 2

$item->assets, not $item['assets']. Do a print_r($json) so you can see the types of the various parts of the JSON – it’ll make them easier to figure out how to access them.

Solution 3

From php official documentation: http://php.net/manual/fr/function.json-decode.php

The 2nd func arg is for assoc array return. You can use it if you prefer to manipulate assoc array over object.

But you actually mix array and object in your loop.

If you keep the arg at TRUE, please use $item['assets']['RAM']

Solution 4

It is parsed as an object, so:

foreach ($item -> assets -> RAM as $asset){ ... }

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply