10

I have this JSON file:

http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json

and I am trying to decode it in PHP as follows:

$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json'); 
$data = json_decode($json);
var_dump($data);

But, the output I am getting is NULL. Am I missing anything?

jeewan
  • 1,597
  • 5
  • 23
  • 40

4 Answers4

8

Using example from PHP.NET json_last_error() I found that your json syntax is not correct:

switch (json_last_error()) {
        case JSON_ERROR_NONE:
            echo ' - No errors';
        break;
        case JSON_ERROR_DEPTH:
            echo ' - Maximum stack depth exceeded';
        break;
        case JSON_ERROR_STATE_MISMATCH:
            echo ' - Underflow or the modes mismatch';
        break;
        case JSON_ERROR_CTRL_CHAR:
            echo ' - Unexpected control character found';
        break;
        case JSON_ERROR_SYNTAX:
            echo ' - Syntax error, malformed JSON';
        break;
        case JSON_ERROR_UTF8:
            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
        break;
        default:
            echo ' - Unknown error';
        break;
}

Output:

 - Syntax error, malformed JSON

However, I checked your json code in the following website but it says valid:

  • Yes, I you are right. I used a new line break \n in my JSON file, that might be causing the problem. However, online JSON validation is good, its not a valid for PHP decoder. – jeewan Mar 21 '13 at 16:18
5

Documentation

json_decode can return NULL as the documentation states.

Returns the value encoded in json in appropriate PHP type. Values true, false and null (case-insensitive) are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

Your Problem

json_decode is failing here because of \n inside of id 6

{ "id": 6, "url": "http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg", "question": "In the movie 'Better Off Dead', what was the \n name of Lane's younger brother?", "answer": [ { "a": { "text": "Bradger", "status": 1 } }, { "b": { "text": "Peter", "status": 0 } }, { "c": { "text": "Frank", "status": 0 } }, { "d": { "text": "Michael", "status": 0 } } ] }

Solution

I guess your best bet here is to escape them before json_decode

$safe_json = str_replace("\n", "\\n", $json);

Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • But I don't know why am I getting NULL, this is a valid JSON. – jeewan Mar 21 '13 at 16:05
  • wow, great catch, thanks. But I am just wondering its a valid JSON. I used \n to get a new line. Shouldn't it be supposed to work? – jeewan Mar 21 '13 at 16:14
  • Escape them? I've added that to the answer. – Rawkode Mar 21 '13 at 16:17
  • Hey Rawkode, I tried the different JSON which does not have any new line character. I tried this one http://jeewanaryal.com/phpTest/jsonTest.json and still says NULL :-(. Looks like \n is not the problem here. – jeewan Mar 21 '13 at 16:25
  • Nope, that decodes fine for me. Remove your `\n` and you'll see it works. If it's still `NULL` for you, then please check your php.ini as suggested by @jeroen – Rawkode Mar 21 '13 at 16:27
  • Ok thank you. I will check that. – jeewan Mar 21 '13 at 16:28
0

Please set the second parameter to true. This will give you an associative array. If the result is still null I'd be interested whether the file_get_contents returned something.

Christoph Grimmer
  • 4,210
  • 4
  • 40
  • 64
0

This is just a guess, but as you do not show what $json contains, I am going to assume that file_get_contents returned false, leading to the result you get.

You should check your php.ini file, what is the value for allow_url_fopen? If that is 0 that means that you cannot read a file from an http address so that could be the problem.

Just change the value of allow_url_fopen to 1 if that is the problem.

jeroen
  • 91,079
  • 21
  • 114
  • 132