Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did the publication of ECMA-404 affect the validity of JSON texts such as '2' or '"hello"'?

Are the following valid JSON texts, or must their top-level value be an array or object?

4.0
"Hello World"
true

Related questions in the past, such as What is the minimum valid JSON?, and Is this simple string considered valid JSON? have concluded that they are not. This was based on the description of the JSON format in RFC-4627, which states that:

2. JSON Grammar

A JSON text is a sequence of tokens. The set of tokens includes six structural characters, strings, numbers, and three literal names.

A JSON text is a serialized object or array.

 JSON-text = object / array

These are the six structural characters:

[...]

However, the RFC-4627 status declares that it "does not specify an Internet standard of any kind". Instead, the official standard for JSON is the recently-published ECMA-404. Unlike RFC-4627, ECMA-404's description of valid JSON text does not include any requirement that it be an object or an array. For example, the section most similar to the quote above is missing that requirement:

4 JSON Text

A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar. The set of tokens includes six structural tokens, strings, numbers, and three literal name tokens.

The six structural tokens:

[...]

Given this new specification, are encoded non-array non-object top-level values considered valid JSON texts?

like image 960
Jeremy Avatar asked Oct 24 '13 14:10

Jeremy


1 Answers

Douglas Crockford posted a comment on this Google+ post which helped me start to clarify things:

JSON is just a grammar, and the grammar includes numbers and strings. Uses of JSON must necessarily be more restrictive. RFC-4627 is one possible use, and was never intended to be the standard for JSON itself.

We cannot say that non-array non-object JSON texts are generally invalid, just that it is not valid to use them with internet media type application/json, per RFC-4627.

Representations of non-object non-array values are valid JSON texts per ECMA-404, which is the only currently published standard that might be identified as "the JSON specification".

However, it turns out that the IETF is likely to soon publish a replacement to RFC-4627 which will also be a specification of JSON. Its latest draft still includes the restriction on JSON texts, but also mentions that JSON has be specified in several places and that these specifications vary slightly. The draft specifically mentions that the definition of JSON in ECMA-262 (the ECMAScript/JavaScript specification) does not share the top-level value restriction.

Therefore, the question of whether non-object non-arrays are valid JSON texts must be disambiguated:

Is "hello" a valid JSON text as specified in RFC-4627 and its successor?

No.

Is "hello" a valid JSON text as specified by ECMA-404 and ECMA-262?

Yes.

like image 130
Jeremy Avatar answered Oct 02 '22 02:10

Jeremy