NotionCommotion Posted March 13, 2017 Share Posted March 13, 2017 Per http://php.net/manual/en/function.json-decode.php: Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit. Okay, so NULL represents a bad json string. If I don't care why it is bad, no need to use json_last_error(), right? What does false represent? It is my understanding that the JSON RFC7159 spec requires all true, false, and null to be lower case. Why does the PHP documentation show these as being returned as uppercase? Quote Link to comment https://forums.phpfreaks.com/topic/303451-returned-values-from-json_encode/ Share on other sites More sharing options...
kicken Posted March 13, 2017 Share Posted March 13, 2017 (edited) A return value of NULL could mean either there was an error, or the json data was just 'null'. Json text does not have to be an array or object at the top level, something like $json = 'null'; is a valid json value and would decode to NULL with no error. Plain true/false values are also valid json which is why those are possible return values. You could also have a simple string or number value. The upper-case version of the TRUE/FALSE/NULL refer to the PHP constants of those values. What they manual is saying is that rather than getting back string "true" it will decode into bool TRUE for example. If you expect to only deal with json that should decode to an array or object, then checking against null would work for detecting an error, otherwise you must check the json_last_error result. If you just check against null you'll want to do a strict check for null otherwise you'd flag an empty array as an error. A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array. Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts. JSON-text = ws value ws ... value = false / null / true / object / array / number / string Edited March 13, 2017 by kicken Quote Link to comment https://forums.phpfreaks.com/topic/303451-returned-values-from-json_encode/#findComment-1544205 Share on other sites More sharing options...
NotionCommotion Posted March 13, 2017 Author Share Posted March 13, 2017 My bad. I incorrectly read the documents to imply that json_decode() will sometimes return false. <?php function jsondecode($json) { var_dump($json); var_dump(json_decode($json)); var_dump(json_last_error()); } jsondecode('[]'); jsondecode('bad'); jsondecode(true); jsondecode(false); jsondecode(null); string(2) "[]" array(0) { } int(0) string(3) "bad" NULL int(4) bool(true) int(1) int(0) bool(false) NULL int(4) NULL NULL int(4) Quote Link to comment https://forums.phpfreaks.com/topic/303451-returned-values-from-json_encode/#findComment-1544206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.