JSON to PHP Array using file_get_contents

hjaffer2001 picture hjaffer2001 · Mar 2, 2012 · Viewed 123.9k times · Source

I am trying to fetch the below json content using a magazine api. The output of the json is like this. i want the below json to convert to php array.

{
"bpath": "http://www.sampledomain.com/",
"clist": [
    {
        "cid": "11",
        "display_type": "grid",
        "ctitle": "abc",
        "acount": "71",
        "alist": [
            {
                "aid": "6865",
                "adate": "2 Hours ago",
                "atitle": "test",
                "adesc": "test desc",
                "aimg": "",
                "aurl": "?nid=6865",
                "weburl": "news.php?nid=6865",
                "cmtcount": "0"
            },

            {
                "aid": "6857",
                "adate": "20 Hours ago",
                "atitle": "test1",
      "adesc": "test desc1",
      "aimg": "",
                "aurl": "?nid=6857",
                "weburl": "news.php?nid=6857",
                "cmtcount": "0"
            }
        ]
    },
    {
        "cid": "1",
        "display_type": "grid",
        "ctitle": "test1",
  "acount": "2354",
        "alist": [
            {
                "aid": "6851",
                "adate": "1 Days ago",
                "atitle": "test123",
      "adesc": "test123 desc",
      "aimg": "",
                "aurl": "?nid=6851",
                "weburl": "news.php?nid=6851",
                "cmtcount": "7"
            },
            {
                "aid": "6847",
                "adate": "2 Days ago",
                "atitle": "test12345",
      "adesc": "test12345 desc",
      "aimg": "",
                "aurl": "?nid=6847",
                "weburl": "news.php?nid=6847",
                "cmtcount": "7"
            }
        ]
    },

]
}

My code looks like this.

<?php 
$json_url = "http://api.testmagazine.com/test.php?type=menu";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>

The above code returns an empty array. :( How is it possible to convert the above JSON to php object array. I am helpless.

Thanks Haan

Answer

P. Galbraith picture P. Galbraith · Mar 2, 2012

The JSON sample you provided is not valid. Check it online with this JSON Validator http://jsonlint.com/. You need to remove the extra comma on line 59.

One you have valid json you can use this code to convert it to an array.

json_decode($json, true);

Array
(
    [bpath] => http://www.sampledomain.com/
    [clist] => Array
        (
            [0] => Array
                (
                    [cid] => 11
                    [display_type] => grid
                    [ctitle] => abc
                    [acount] => 71
                    [alist] => Array
                        (
                            [0] => Array
                                (
                                    [aid] => 6865
                                    [adate] => 2 Hours ago
                                    [atitle] => test
                                    [adesc] => test desc
                                    [aimg] => 
                                    [aurl] => ?nid=6865
                                    [weburl] => news.php?nid=6865
                                    [cmtcount] => 0
                                )

                            [1] => Array
                                (
                                    [aid] => 6857
                                    [adate] => 20 Hours ago
                                    [atitle] => test1
                                    [adesc] => test desc1
                                    [aimg] => 
                                    [aurl] => ?nid=6857
                                    [weburl] => news.php?nid=6857
                                    [cmtcount] => 0
                                )

                        )

                )

            [1] => Array
                (
                    [cid] => 1
                    [display_type] => grid
                    [ctitle] => test1
                    [acount] => 2354
                    [alist] => Array
                        (
                            [0] => Array
                                (
                                    [aid] => 6851
                                    [adate] => 1 Days ago
                                    [atitle] => test123
                                    [adesc] => test123 desc
                                    [aimg] => 
                                    [aurl] => ?nid=6851
                                    [weburl] => news.php?nid=6851
                                    [cmtcount] => 7
                                )

                            [1] => Array
                                (
                                    [aid] => 6847
                                    [adate] => 2 Days ago
                                    [atitle] => test12345
                                    [adesc] => test12345 desc
                                    [aimg] => 
                                    [aurl] => ?nid=6847
                                    [weburl] => news.php?nid=6847
                                    [cmtcount] => 7
                                )

                        )

                )

        )

)