

I just had jsondecode() returning null because the string contained one of those special en-dashes, probably pasted from MS Word, and then maybe mis-encoded. sapi/cli/php test-45989.phpĬonfiguration File (php.ini) Path: /usr/local/lib Also note this can happen with other invalid characters in the middle of the file. I also build the most recent 5.2.7 snapshot: I double-checked some of Steve's examples on (which is in most docs cited as the reference validator for json data) and they all show up as "invalid". If the function returns "whatever" then the docs should be updated to tell the user to not rely on what is returned by json_decode at all. PHP answers related to php json encode non json string php jsonencode indent javascript function in php jsonencode php try to decode json php array json. this bug be re-evaluated or a more detailed explaination as of why the docs sometimes note that "NULL" is returned on invalid json, and why sometimes json_decode() returns the string instead? how would you currently see if json_decode() succeeded or not? But because of how inconsistent it is now, youĬan't say that it is by design or following any kind of spec.Į.g. If this was merely a case of json_decode() returning /all/ invalid json as is, then it couldīe classified as an implementation quirk. Suspiciously like a bad parser implementation. Json_decode() arbitarily returns /some of it/ as a string. Youre taking a string of JSON and JSON encoding it, aka double JSON encoding it, which doesnt. With this in mind, we can see some critical mistakes are made in the code, caused by muddling up the two. jsondecode takes a JSON string and turns it into a PHP array/object/etc. Most widely stretched definition of JSON, the above is not JSON encoded data. jsonencode takes an array/object and turns it into a JSON string. Please examine the examples closely: they are all meaningless, invalid JSON. Yet the following each returns the literal argument as a string:

PHP JSON DECODE A NON JSON STRING CODE
Var_dump(json_decode('')) // invalid escape code Var_dump(json_decode('')) // unclosed string Var_dump(json_decode('')) // invalid object key/value notation Var_dump(json_decode('[')) // unmatched bracket The following each returns NULL, as expected: steven at acko dot net Please clarify the bogus classification. (The non-standard behaviour introduced in #38680 is not documented atĪll by the way, which is kind of ironic given the numerous issues that Strings can not be considered valid, and trivial variations on them do This bug report isĭifferent because even under the PHP interpretation of JSON, these ) even though this is not allowedīy RFC 4627 (which only allows objects/arrays). Note: in #38680, the decision was made to allow json_decode() to accept The literal string as the result, rather than returning NULL. When json_decode() is given certain invalid JSON strings, it will return
