Back to blog
Fundamentals8 min read

How to Access Nested JSON Data Without Losing Your Mind

Read deeply nested JSON safely: dot and bracket access, arrays inside objects, guarding against undefined and null, optional chaining, JSONPath queries, and flattening for tabular use.

By Saifur Rahman Shochib

Real-world JSON is rarely flat. API responses nest objects inside objects inside arrays, and reaching the one value you need — without crashing on a missing field — is a daily task. This guide covers how to access nested JSON safely across languages, the patterns that prevent null-reference errors, and the tools that make deep structures navigable.

Dot and bracket access

Once JSON is parsed into objects and arrays, you reach nested values by chaining steps. Dot notation walks object keys: response.user.address.city. Bracket notation does the same and is required when a key has spaces or special characters: response["user"]["address"]["city"]. Array elements use a numeric index along the way: response.items[0].price.

Reaching values by key and indexjavascript
const data = {
  user: { address: { city: "Paris" } },
  items: [{ price: 9.99 }, { price: 4.5 }],
};

data.user.address.city;        // "Paris"
data["user"]["address"]["city"]; // "Paris"
data.items[0].price;           // 9.99

Arrays inside objects, objects inside arrays

The trickiest navigation mixes the two. To read the email of the second user in a users array, you combine an index and a key: data.users[1].email. To process every element, loop over the array and read the key from each item. Knowing whether each level is an object (use a key) or an array (use an index) is the whole skill.

Guarding against missing data

The danger of deep access is that any intermediate step might be missing, and reading a property of undefined or null throws. The fix in JavaScript is optional chaining: data?.user?.address?.city returns undefined instead of throwing if any link is absent, and the nullish coalescing operator supplies a default: data?.user?.name ?? "Unknown". Python's equivalent is dict.get with defaults or a try/except, and many languages offer similar safe-navigation patterns.

Optional chaining avoids 'cannot read property of undefined'javascript
data?.user?.address?.city;          // "Paris"
data?.account?.plan?.name ?? "free"; // default when any link is missing

Querying with JSONPath

When you want many nested values at once, manual traversal is tedious. JSONPath expresses the query in one string: $..email finds every email anywhere in the document, and $.store.book[?@.price < 10].title selects nested values by condition. It is the JSON analogue of XPath and is built into many tools and libraries for exactly this purpose.

JSONPath queries the whole document at oncetext
$..city                  → every "city" at any depth
$.items[?@.price < 10]   → items cheaper than 10

Flattening deeply nested JSON

Sometimes the cleanest fix is to stop nesting. Flattening collapses a nested object into single-level keys like user.address.city, which is ideal for CSV export, spreadsheets, and key-value stores. A JSON Flatten tool does this with a dot or underscore delimiter, and the Unflatten tool rebuilds the hierarchy when you need it back.

See the structure first

Before writing access code, look at the shape. A tree viewer collapses branches so you can see exactly how deep a value sits and whether each level is an object or an array, and a formatter makes the raw JSON readable. Knowing the path in advance prevents the trial-and-error that causes undefined errors.

Frequently asked questions

How do I access a value in nested JSON?

Chain the steps from the root: use a key for each object level and an index for each array level, for example data.users[0].address.city. Bracket notation like data["users"][0] works too and is needed for keys with special characters.

How do I avoid 'cannot read property of undefined' with nested JSON?

Use optional chaining in JavaScript: data?.user?.address?.city returns undefined instead of throwing when a link is missing. Combine it with the nullish coalescing operator (??) to supply a default value.

How do I get all values of a key from nested JSON?

Use a JSONPath recursive query: $..email returns every email field at any depth. Many languages and tools support JSONPath for exactly this kind of bulk extraction.

What is the easiest way to read deeply nested JSON?

View it as a tree first so you can see the path to your value, then use safe navigation (optional chaining) in code. For tabular needs, flatten the JSON into dotted single-level keys.

How do I flatten nested JSON?

Use a flatten tool that collapses nested keys into a single level with a dot or underscore delimiter, turning user.address.city into one key. This is ideal for CSV and spreadsheet export, and unflattening reverses it.

Related tools

Try these tools

Keep reading

Related guides