I'm working with an old system, that sends me some JSON.
I was stuck with a bug for a while, where if I parsed using json_decode( $json_string, true);
that it then returned null
.
I found the solution here, which was to do this:
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
I don't get why that worked, though. Googling around for those characters tell me, that it's some 'unreadable characters'. But I can't figure out what purpose they serve; why they are there.
And... It makes me in doubt, every time I'm decoding a JSON-string now, with this system, having to print_r
it, to ensure that the parsed JSON isn't null (because it's not always that the JSON contains the bad characters).
And even further, then I can read in some of the comments, where I found the solution, that one should be careful removing those characters.
I'm retrieving the JSON-string, by doing this: file_get_contents( 'https://example.org/the-endpoint' );
So this leads to to three questions:
- What errors could I stumble into, if I always json_decoded like this?
function improved_json_decode( $json_string ){
return json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $json_string), true );
}
Why does this happen in the first place? Does elves add the characters or what is going on?
Should I (and can I) check a JSON-string, if it's "parsable"?