CheckMySchema

Structured data explained

The difference between {} and [] in JSON

A clear explanation of what {} and [] mean in JSON, when to use objects vs arrays, key syntax rules, and common pitfalls to avoid.

    Minimal cyan and midnight editorial diagram comparing a JSON object on left and a JSON array on right with arrows showing keyed versus positional access

    {} starts a JSON object (an unordered collection of name-value pairs), and [] starts a JSON array (an ordered sequence of values). Use {} when you need named fields accessed by key, and [] when order or position matters RFC 8259.

    What {} and [] mean in JSON

    JSON defines only two structured types: objects and arrays. Objects are enclosed in curly braces and contain name-value pairs; arrays are enclosed in square brackets and contain values in a specific order Java EE JSON introduction, RFC 8259.

    Minimal, valid examples

    An object with two named fields:

    {
      "user": "alice",
      "id": 42
    }

    An array with three ordered values:

    [
      "alice",
      "bob",
      "carol"
    ]

    Names in objects are strings and must be in double quotes. Strings must also use double quotes. Trailing commas and comments are not part of JSON RFC 8259.

    Close up full frame diagram showing valid versus invalid JSON snippets side by side highlighting double quoted keys in blue and forbidden trailing commas in red

    Key differences that affect how your data behaves

    Ordering

    • Arrays are ordered: element order is significant.
    • Objects are unordered: do not rely on member order. Parsers may expose insertion order, but interoperable software should not depend on it RFC 8259.

    Access pattern

    • Objects are keyed: obj["email"] reads a named field.
    • Arrays are positional: items[0] reads the first element.

    Duplicates and mixed types

    • Avoid duplicate member names in an object. Behavior is unpredictable across implementations (some keep the last, some error) RFC 8259.
    • Arrays may mix types (numbers, strings, objects, arrays, booleans, null) RFC 8259.

    How to choose: simple rules

    • Use {} when each field has a meaningful name and consumers look up values by key.
    • Use [] when sequence, iteration, or indexing matters.
    • Combine them: for example, an object with metadata plus an items array.
    {
      "total": 2,
      "items": [
        {"id": 1, "name": "A"},
        {"id": 2, "name": "B"}
      ]
    }
    

    Quick checklist: object vs array

    Common mistakes and quick fixes

    Quoting and punctuation

    • Property names must be double-quoted strings: valid {"name": "Ada"}, invalid {name: "Ada"} RFC 8259.
    • Strings must use double quotes: valid "London", invalid 'London' RFC 8259.
    • No trailing commas: valid {"a":1, "b":2}, invalid {"a":1, "b":2,} RFC 8259.
    • No comments in JSON. Remove them or carry them in separate documentation RFC 8259.

    Structure choice pitfalls

    • Needing a stable sequence? Use an array, not an object.
    • Needing named fields? Use an object; do not pack names into an array by position.
    • Avoid duplicate keys in an object to prevent parser-dependent behavior RFC 8259.
    Minimalist 2D vector diagram showing a top level JSON object with a metadata panel on the left and three stacked item cards on the right on a midnight background

    Where to go next

    FAQ

    Can a JSON text be something other than an object or array?

    Yes. A JSON text is any serialized value. For widest interoperability, many systems still exchange top-level objects or arrays RFC 8259.

    Can arrays mix types in JSON?

    Yes. Arrays may contain values of different types, including other arrays and objects RFC 8259.

    Does an object preserve the order of its members?

    Order is not significant for objects and should not be relied on. Parsers differ in whether they expose member order RFC 8259.

    CheckMySchema logo

    Related guides

    Official sources and further reading