1

I pull data from a json file on the remote server. This json file has 97000 Lines of json code. It returns null when I decode the Json file. When I debug the Json errors, I see that there is no error.

Json file : https://opendata.ecdc.europa.eu/covid19/casedistribution/json/

$json = file_get_contents("https://opendata.ecdc.europa.eu/covid19/casedistribution/json/");
$json =  json_decode($json, true);
var_dump($json); // Return Null

But when I decode another json file there is no error

$json = file_get_contents("https://randomuser.me/api/");
$json =  json_decode($json, true);
var_dump($json); // Return Array

Could this be due to the size of the data?

Thanks for advance

2
  • When I check for errors (json_last_error_msg()), I get "Syntax error"… Commented Apr 1, 2020 at 15:18
  • 2
    If there is an error, you should include it in the question. Commented Apr 1, 2020 at 15:19

1 Answer 1

4

The file starts with a BOM, which is a syntax error for json_decode.

Ultimately this should be fixed by the host, as a workaround you can strip the first three bytes:

if (substr($json, 0, 3) == "\xEF\xBB\xBF") {
    $json = substr($json, 3);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.