Home > Article > Web Front-end > What are the Different Definitions of Valid JSON?
Understanding the Minimal Valid JSON
The concept of JSON has been widely discussed in various RFCs and specifications. RFC4627 initially defined JSON as being either a serialized object or array. Based on this definition, only {} (empty object) and [] (empty array) qualify as valid, complete JSON strings.
However, ECMA-404 introduced an amendment that expanded the range of valid JSON strings. With this update, string, number, true, false, null, {} (empty object), and [] (empty array) are all considered valid JSON strings.
In the realm of web browsers, JSON is standardized in ES5. ES5 defines valid JSON texts as the same as RFC 4627 with two exceptions. Firstly, the top-level JSONText production may consist of any JSONValue (including strings, numbers, booleans, and null) instead of being restricted to an object or array. This means that all JSON values are accepted in web browsers, even though the underlying JSON object may adhere to RFC 4627.
It's important to note that different parsers may have varying levels of support for the full range of valid JSON strings. For instance, Ruby only accepts objects and arrays as the root, while PHP expressly includes scalar types and NULL in its encoding and decoding capabilities.
The above is the detailed content of What are the Different Definitions of Valid JSON?. For more information, please follow other related articles on the PHP Chinese website!