MediumExperience level: 3-5 years · 8-15 years1 min read

Accessing Nested JSON — Interview Questions

Reviewed by Gurusankar M.

Asked inAmazonMicrosoftDeloitteCognizant
#json#nested access#optional chaining#dot notation#safe access
Report issue

Short Answer

After parsing JSON into an object, drill in with dot or bracket notation (obj.a.b, obj["a"]["b"]) and array indices (arr[0]). Because any level can be missing, access defensively — optional chaining (a?.b?.c) with a default in JS, or dict.get()/try-except in Python — so a missing branch returns a fallback instead of crashing.

On this page

Concept Made Simple

Coffee Chat Question

“How do you safely access values in nested JSON?”

Remember It Faster

Mind Map Answer

Dot
obj.address.city
Bracket
obj["address"]["city"] (dynamic keys)
Array
obj.orders[0].id
Safe
obj?.address?.city ?? 'N/A'

Learn by Doing

Hands-on Keyboard

javascript
const data = JSON.parse('{"user":{"address":{"city":"Chennai"}}}');

// safe: won't throw if a level is missing
const city = data.user?.address?.city ?? "unknown";
const zip  = data.user?.address?.zip  ?? "unknown";
console.log(city, zip);
Output
Chennai unknown

Think Beyond the Expected

What If?

Why does `data.user.address.city` throw when `address` is missing?

Reading `.city` on `undefined` throws 'Cannot read properties of undefined'. Each dot dereferences the previous result, so one missing level breaks the chain. Optional chaining (`?.`) short-circuits to undefined at the first gap, and `?? default` supplies a fallback — the safe idiom for untrusted payloads.

Real World

Third-party and optional API fields are the top cause of 'Cannot read property of undefined' in production. Optional chaining plus sensible defaults is the everyday defense when consuming JSON you don't fully control.

Interviewer's Expectation

Keywords they're listening for:

  • dot vs bracket notation
  • array index access
  • optional chaining `?.`
  • defaults with `??`
  • Python dict.get / try-except

Common Mistakes

  • Chained dot access with no null checks
  • Bracket vs dot confusion with dynamic keys
  • Assuming optional fields are always present

Best Practices

  • Use optional chaining + defaults for untrusted data
  • Validate shape before deep access
  • Prefer dict.get()/JSONPath for dynamic keys

Follow-up Questions

  1. How do you do the same safely in Python?
  2. When would you flatten instead of deep-accessing?
  3. What tools query JSON (JSONPath, jq)?

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: Objects & Arrays (JSON)
Interview question: "How do you safely access values in nested JSON?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.