1

I have the following code that converts json into an array:

$str = file_get_contents('http://localhost/data.json');
$decodedstr = html_entity_decode($str);
$jarray = json_decode($decodedstr, true);


echo "<pre>";
print_r($jarray);
echo "</pre>";

but my $jarray keeps returning null... I dont know why this is happening.. i have already validated my json in this question: validated json question could anyone tell me what i am doing wrong? or what is happening. Thanks in advance.

when i echo my $str i get the following:image of echo of $str

Community
  • 1
  • 1
FutureCake
  • 2,614
  • 3
  • 27
  • 70

2 Answers2

1

You are passing to json_decode a string that is not valid JSON, this is the reason NULL is returned.

As I see from comments inspecting the error code generated gives 4 that corresponds to the constant JSON_ERROR_SYNTAX that just means the JSON string has a Syntax Error.

(See http://php.net/manual/en/function.json-last-error.php)


You should inspect (echo) what you get from

$str = file_get_contents('http://localhost/data.json');

(You may edit your answer and post it - or a portion of it)

For sure it is not valid JSON; the problem lies there: in data.json.

Then as you fix things and get from data.json what is expected I would ensure you really need to use html_entity_decode on the fetched data.

It would be "weird" to have html encoded JSON data.


UPDATE

Looking at what you get from data.json it seem the JSON data contains actually HTML entities (as I see the presence of &nbsp;s)

This is actually weird, the right thing to do would be to fix how data.json is generated ensuring non-html-encoded JSON data is returned, charset is UTF-8 and the response content type is Content-Type: application/json.

We can't deepen this here as I don't know where does data.json come from or the code that generates it. Eventually you may post another answer.

So here is a quick fix provided that the right approach would be what I just suggested above.

As you decode html entities, non breaking spaces &nbsp; turns into 2 byte UTF-8 characters (byte values 196, 160) that are not valid for JSON encoded data.

The idea is to remove these characters; your code becomes:

$str = file_get_contents('http://localhost/data.json');
$decodedstr = html_entity_decode($str);

// the character sequence for decoded HTML &nbsp;
$nbsp = html_entity_decode( "&nbsp;" );

// remove every occurrence of the character sequence
$decodedstr = str_replace( $nbsp, "", $decodedstr );

$jarray = json_decode($decodedstr, true);
Paolo
  • 15,233
  • 27
  • 70
  • 91
0

from php manual

http://php.net/manual/en/function.json-decode.php

Return:

... NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit

So, surely the JSON string passed to json_decode() is not valid:

maybe because of html_entity_decode

Hossam
  • 1,126
  • 8
  • 19
  • so if my json exceeds the recursion limit how do i fix this? the data i get is from a client. So i probably cant ask him to change the json. – FutureCake May 07 '17 at 11:32
  • @FutureCake the error is not caused by excessive recursion but by a syntax error (see my answer for details) – Paolo May 07 '17 at 11:39