{}JSON FYI

JSON objects — syntax, nesting, and common mistakes

How JSON objects work: key rules, nesting, accessing values in JavaScript and Python, duplicate keys, and the mistakes that cause parse errors.

·6 min read

What is a JSON object?

A JSON object is an unordered collection of key/value pairs wrapped in curly braces — for example {"name": "Ada", "age": 36}. Keys must be double-quoted strings, each key is separated from its value by a colon, pairs are separated by commas, and there is no trailing comma after the last pair.

Syntax rules

{ "key": value, "key2": value2 }
  • Curly braces wrap the object: { and }
  • Keys must be strings in double quotes — not single quotes, not bare words
  • A colon separates each key from its value
  • Commas separate pairs; no trailing comma after the last one
  • Values may be any JSON type: string, number, boolean, null, array, or object
  • An empty object {} is valid

Examples

// Simple
{ "name": "Ada", "age": 36, "active": true }

// Nested objects
{
  "user": {
    "name": "Ada",
    "address": { "city": "London", "zip": "SW1A" }
  }
}

// Object containing an array of objects
{
  "team": "platform",
  "members": [
    { "id": 1, "name": "Ada" },
    { "id": 2, "name": "Grace" }
  ]
}

// Empty object
{}

Accessing values

// JavaScript
const data = JSON.parse('{"user":{"name":"Ada","tags":["a","b"]}}');
data.user.name;            // "Ada"
data.user.tags[1];         // "b"
data.user?.address?.city;  // undefined — safe optional chaining
Object.keys(data.user);    // ["name", "tags"]

# Python
import json
data = json.loads('{"user":{"name":"Ada","tags":["a","b"]}}')
data["user"]["name"]           # "Ada"
data["user"].get("address")    # None — no KeyError
list(data["user"].keys())      # ['name', 'tags']

Common mistakes

Unquoted or single-quoted keys

// ✗ Invalid — bare key (this is a JS object literal, not JSON)
{ name: "Ada" }

// ✗ Invalid — single quotes
{ 'name': 'Ada' }

// ✓ Valid
{ "name": "Ada" }

Trailing comma

// ✗ Invalid
{ "a": 1, "b": 2, }

// ✓ Valid
{ "a": 1, "b": 2 }

Duplicate keys

Legal but dangerous — most parsers silently keep the last value. See duplicate keys in JSON for what each parser does.

{ "id": 1, "id": 2 }   // parses to { "id": 2 } in most languages

Python/JS literals that aren't JSON

// ✗ None / True / False are Python, not JSON
{ "value": None, "ok": True }

// ✓ JSON uses lowercase and null
{ "value": null, "ok": true }

Objects vs arrays — which to use

Use an object when each value has a meaningful name. Use an array when you have a list of like items and order matters. A very common API shape combines both: an object of metadata wrapping an array of records.

{
  "total": 2,
  "page": 1,
  "results": [
    { "id": 1, "name": "Ada" },
    { "id": 2, "name": "Grace" }
  ]
}

See the companion guide on JSON arrays for the list side of this.

Explore the structure in the tree viewer →

Expand nested arrays and objects, see every type at a glance, and click any node to copy its JSONPath.

Open Tree Viewer →

Frequently asked questions

Can JSON object keys be numbers?+

Not as bare numbers. Keys must always be double-quoted strings, so write {"1": "a"} rather than {1: "a"}. The key is then the string "1", not the number 1.

Are JSON objects ordered?+

The specification treats objects as unordered collections, so you should never depend on key order. In practice most parsers preserve insertion order (JavaScript and Python 3.7+ both do), but jsonb in Postgres deliberately reorders keys.

Is an empty object {} valid JSON?+

Yes. {} is a valid, complete JSON document representing an object with no properties.

How deeply can JSON objects nest?+

The spec sets no limit, but parsers do. Most handle far more nesting than real data needs; documents beyond about 32 levels can hit implementation limits and are usually a sign the shape should be flattened.

What is the difference between a JSON object and a JavaScript object?+

JSON is a text format; a JavaScript object is an in-memory value. JSON requires double-quoted keys and allows only six value types, whereas JavaScript objects allow unquoted keys, functions, undefined, symbols, comments, and trailing commas.

Related tools & guides