Search code examples
jsonstandards

Does the top-level value in JSON have to be an array or object?


This object literal is valid JSON:

{ "foo" : "bar"}

And so is this array literal:

["foo", "bar", 1, 2, 3, null]

But what about a string by itself?

"foo"

Or a number by itself?

 0

Solution

  • Recent JSON specs (RFC 7159 Mar 2014, and ECMA-404) define a JSON text as optional whitespace surrounding any JSON value, where a value is any one of:

    • object
    • array
    • string
    • number
    • true
    • false
    • null

    (see the start of sections 2. JSON Grammar, and 3. Values)

    Older JSON specs (RFC 4627, obsoleted) only allowed the top-level element to be an object or an array.