Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode returns NULL, json_last_error_msg gives "Control character error, possibly incorrectly encoded"

Tags:

json

php

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.

like image 681
Dean Or Avatar asked Jun 19 '14 17:06

Dean Or


1 Answers

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();
like image 151
jk2K Avatar answered Oct 11 '22 18:10

jk2K