[php] JSON to PHP Array using file_get_contents

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

This question is related to php arrays file-get-contents json

The answer is


Check some typo ','

<?php
 //file_get_content(url);
$jsonD = '{
    "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"
                }
            ]
        }
    ]
}
';

$parseJ = json_decode($jsonD,true);

print_r($parseJ);
?>

You JSON is not a valid string as P. Galbraith has told you above.

and here is the solution for it.

<?php 
$json_url = "http://api.testmagazine.com/test.php?type=menu";
$json = file_get_contents($json_url);
$json=str_replace('},

]',"}

]",$json);
$data = json_decode($json);

echo "<pre>";
print_r($data);
echo "</pre>";
?>

Use this code it will work for you.


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to file-get-contents

file_get_contents(): SSL operation failed with code 1, Failed to enable crypto get url content PHP PHP cURL vs file_get_contents Does file_get_contents() have a timeout setting? JSON to PHP Array using file_get_contents Why doesn't file_get_contents work? Get file content from URL? Show image using file_get_contents Alternative to file_get_contents? PHP ini file_get_contents external url

Examples related to json

Use NSInteger as array index Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>) HTTP POST with Json on Body - Flutter/Dart Importing json file in TypeScript json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 190) Angular 5 Service to read local .json file How to import JSON File into a TypeScript file? Use Async/Await with Axios in React.js Uncaught SyntaxError: Unexpected token u in JSON at position 0 how to remove json object key and value.?