Home > Article > Web Front-end > What is the Minimum Valid JSON, and Does It Depend on the Parser?
Minimum Valid JSON: A Journey Through Standards Compliance
The definition of valid JSON has been a topic of debate due to evolving standards. According to RFC4627, the foundational document for JSON, only empty objects ({}) and empty arrays ([]) qualify as complete JSON strings.
However, ECMA-404 brought about a significant change by allowing any JSONValue, including strings, numbers, and nulls, at the top level. This means that in parsers and stringifiers adhering to ECMA-404, the minimum valid JSON is as follows:
However, web browsers implement JSON using ES5, which also allows scalar types and NULL as valid JSON. As a result, browsers may accept:
It's important to note that some parsers, such as Ruby, may only accept objects and arrays as root elements, adhering strictly to RFC4627. PHP, on the other hand, explicitly allows scalar types and NULL.
Therefore, the minimum valid JSON depends on the specific parser or stringifier being used. To ensure compatibility across different implementations, it's recommended to always use JSON objects or arrays as the root of your JSON data.
The above is the detailed content of What is the Minimum Valid JSON, and Does It Depend on the Parser?. For more information, please follow other related articles on the PHP Chinese website!