8
votes

The file looks fine when read into my editor.

$file = file_get_contents('path/to/file.json');
$json =  json_decode($file, true);
var_dump($json); // null
echo json_last_error_msg(); //Control character error, possibly incorrectly encoded

There isn't much out there on what this error message means.

1
Could you please post your file.json ? because json_decode fails to decode your json string from file.Ravi Dhoriya ツ
It means your file doesn't contain valid JSON.Felix Kling

1 Answers

22
votes

you can remove the control character, PCRE supports the POSIX notation for character classes [:cntrl:]

$json = preg_replace('/[[:cntrl:]]/', '', $json);
$json = json_decode($json, true);
var_dump($json);
echo json_last_error_msg();