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

1

21

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?

Libna answered 24/10, 2013 at 14:48 Comment(3)
I believe this question is a duplicate of stackoverflow.com/questions/18419428, because the question is the same, even though the context (date on which it was asked) is different.Finsen
As per RFC7159 A JSON text is a serialized value in other words all valid JSON values are valid JSON text i.e. false / null / true / object / array / number / string are all valid JSON textSimilar
Deleting this post because it's kind-of a duplicate and not really relevant in 2019.Libna
L
31

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.

Libna answered 24/10, 2013 at 15:50 Comment(2)
Tim Bray wrote an informative post that explains more of the history of JSON specification.Libna
See my answer to #18419928 which updates the definition found in RFC-7158 which does allow a string as the top-level JSON text. That answer also clarifies what RFC-7159 is.Finsen

© 2022 - 2024 — McMap. All rights reserved.