1

I've been learning a bit about JSON lately and was trying to understand if it always needs curly braces at the top level or if it can also be an array? Or would the array have to be wrapped in curly braces with a key name and then the array as a value?

For instance, does it have to be this:

{"title":"title 1"}

or could it be this as well:

[1,2,3]

I'm asking in the context of what the spec allows and consumers of the json file might expect, as typically from examples I've seen it, it's always curly braces with key-value pairs inside

j obe
  • 1,759
  • 4
  • 15
  • 23

3 Answers3

3

The original specification said:

A JSON text is a serialized object or array.

… meaning that the top level needed to be either {} or [].

Many implementations ignored that restriction and allowed any JSON data type (object, array, number, string, boolean, null) to be used at the top level.

The updated specification says:

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.

So now any JSON data type is allowed at the top level, but you need to be aware that some older software might not support anything except objects and arrays at the top level.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • ```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``` What is meant by this? – j obe Dec 24 '20 at 18:24
  • also my follow up @Quentin then is how would json files of other types be consumed, are there examples of this or is it just a case that it's allowed but nobody ever does. For instance if JSON could just contain a single string, or set of comma separated strings, would that be practical at all? As I've only ever seen it used with {} – j obe Dec 24 '20 at 18:26
  • @jobe — It means that an old serializer that can only generate `{}` or `[]` based JSON will produce JSON that can be parsed by a parser that supports the new standard. – Quentin Dec 24 '20 at 18:33
  • "For instance if JSON could just contain a single string" - Then a parser would output a string – Quentin Dec 24 '20 at 18:33
  • "or set of comma separated strings" — Then a parser would throw an exception.That wouldn't be valid JSON. – Quentin Dec 24 '20 at 18:33
  • "would that be practical at all?" — Probably. Somewhere. I'm not immediately thinking of any usecases. – Quentin Dec 24 '20 at 18:35
  • Thanks, that helps explain a few things I was unsure of as typically while learning about API's I've only seen the curly object form of JSON. – j obe Dec 24 '20 at 20:21
0

It can be array, need not to be in curly braces

console.log(JSON.stringify([1,2,3]));

Here's more

Sudhanshu Kumar
  • 1,926
  • 1
  • 9
  • 21
0

it can be of the following:

  1. null
  2. boolean
  3. number
  4. array
  5. object
  6. String: only if its enclosed in quotes

Examples:

JSON.parse('{}');              // {}
JSON.parse('true');            // true
JSON.parse('"foo"');           // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null');            // null
Jasneet Dua
  • 3,796
  • 2
  • 10
  • 15