0

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:

  1. 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 );
}
  1. Why does this happen in the first place? Does elves add the characters or what is going on?

  2. Should I (and can I) check a JSON-string, if it's "parsable"?

El_Vanja
  • 3,022
  • 4
  • 16
  • 21
Zeth
  • 1,662
  • 3
  • 35
  • 72
  • 1
    Basically, if its JSON (valid) then it will work nicely using the standard PHP functions. You can test for errors in the conversion using `json_last_err()` and `json_last_err_msg()` to see if the conversion failed. – RiggsFolly May 10 '21 at 13:20
  • 2
    3. This is exactly what [`json_decode()`](https://www.php.net/manual/en/function.json-decode) does for a living. It returns `null` in two situations: when the input string is exactly `null` (this probably does not happen in your case) and when the input string cannot be parsed for some reason. – axiac May 10 '21 at 13:20
  • 1
    As we are not really sure what issue caused you to try the preg_replace to fix, more is difficult to say. Would you let us know what the issue was? – RiggsFolly May 10 '21 at 13:21
  • 2
    My one thought was that the JSON might have a BOM at the front for some reason. – Chris Haas May 10 '21 at 13:22
  • 2) is likely a question for whoever generates the JSON that the endpoint returns. – El_Vanja May 10 '21 at 13:26

0 Answers0