Nested JSON structures explained - cover art

JSON 13 min read

Nested JSON structures explained

October 28, 2026 · 13 min read

Real APIs rarely flatten to one level. Orders contain line items, users embed addresses, and webhooks wrap payloads in envelopes. Nested JSON expresses trees of objects and arrays; bugs appear when code assumes a field is always present or confuses array index with object key.

Nesting basics

JSON value types compose recursively: an object’s values can be objects or arrays, and arrays can hold any JSON value including nested structures. Depth is unlimited in the spec; practical limits come from parser stack, payload size, and human readability.

Objects inside objects

{
  "user": {
    "id": "9b2c…",
    "profile": {
      "displayName": "Ada",
      "settings": { "theme": "dark", "notifications": true }
    }
  }
}

Access paths in code look like user.profile.settings.theme in JavaScript or optional chaining when intermediate keys may be missing. Document optional branches in JSON Schema so clients know when profile can be null.

Arrays of objects and nested arrays

Arrays preserve order - use them for timelines and line items. An array of objects is the standard pattern for tables in JSON. Nested arrays appear in matrices and GeoJSON coordinates; compare carefully in tests because deep equality is easy to get wrong.

JSON Pointer paths and tooling

JSON Pointer notation (/user/profile/displayName) names nodes for PATCH and validation errors. When diffing nested trees, use the JSON compare tool to highlight path-level changes instead of line noise from pretty-print.

Common nested JSON pitfalls

FAQ

How deep should I nest JSON?
As shallow as clarity allows. More than 4–5 levels hurts readability; split resources or use links by ID.
Are object keys ordered?
JSON objects are unordered semantically; parsers may preserve insertion order but do not rely on it for equality.
Can I query nested JSON in SQL?
Yes - PostgreSQL jsonb operators and MongoDB dot notation traverse nested paths.

Related: JSON vs BSON · How to compare JSON objects · JSON diffing explained · How to debug JSON APIs

Browse all tools