Home  >  Article  >  Web Front-end  >  What is the Minimum Valid JSON, and Does It Depend on the Parser?

What is the Minimum Valid JSON, and Does It Depend on the Parser?

Susan Sarandon
Susan SarandonOriginal
2024-11-04 08:57:30729browse

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:

  • {}
  • []
  • "string"
  • 42 (a number)
  • true
  • null

However, web browsers implement JSON using ES5, which also allows scalar types and NULL as valid JSON. As a result, browsers may accept:

  • 5 (a number)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn