12

Are JSON values string, number, true, false, null valid JSON? I.e., is

true

a valid JSON document? Or does is have to be an array/object?

Some validators accept this (e.g. http://jsonlint.com/), while others do not (e.g. http://jsonschemalint.com/). The RFC and json.org are not clear on this issue.

Jonny5
  • 1,390
  • 1
  • 15
  • 41
  • The RFC is indeed clear on this issue. – djechlin Mar 04 '13 at 16:40
  • This has been re-asked [here](http://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json/18419527) and received more detailed answers. I propose this be marked as a duplicate and the newer question kept. – IMSoP Aug 24 '13 at 14:37
  • Possible duplicate of [What is the minimum valid JSON?](https://stackoverflow.com/questions/18419428/what-is-the-minimum-valid-json) – Charles Duffy Feb 10 '19 at 03:34

2 Answers2

10

As of March 2014: Yes. From the specification:

A JSON text is a serialized value. Note that certain previous specifications of JSON constrained a JSON text to be an object or an array. Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts.

However, at the time this question was originally asked, the answer was: No. The original specification said:

A JSON text is a serialized object or array

So the outer-most data type in a JSON text had to be either an object or an array, it couldn't be a string, boolean, number or any other data type.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    While this answer is correct according to the official spec, it's interesting to note that JSON.parse(42) seems to work in browsers. – bobics Jul 03 '13 at 19:15
  • 1
    Just a technical note - the RFC you mentioned is for "The application/json Media Type for JavaScript Object Notation". Not for the JSON format as such. Neither http://www.json.org/ nor ECMA-404 state this (afaik). I personally find it weird that [42] is valid JSON and simple 42 is not. – Elephantik Jul 01 '14 at 13:58
  • See http://stackoverflow.com/questions/19569221/did-the-publication-of-ecma-404-affect-the-validity-of-json-texts-such-as-2-or (noting the publication date of ecma-404 and this answer). – Quentin Jul 01 '14 at 14:13
0

Yes, according to ECMA-404 The JSON Data Interchange Standard.

Source: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf

A JSON text is a sequence of tokens formed from Unicode code points that conforms to the JSON value grammar.

And following, the JSON value grammar is given as:

A JSON value can be an object, array, number, string, true, false, or null.

This directly conflicts with the RFC, as cited by @quentin. The updated RFC is 8259 which states:

Note that certain previous specifications of JSON constrained a JSON text to be an object or an array. Implementations that generate only objects or arrays where a JSON text is called for will be interoperable in the sense that all implementations will accept these as conforming JSON texts.

William Entriken
  • 37,208
  • 23
  • 149
  • 195