Medium👤 3-5 years👤 8-15 years 1 min read

Flattening JSON — Interview Questions

Asked inAmazonDeloitteCognizantWipro
#json#flatten#nested#transformation#etl
Report issue

⚡ Short Answer

Flattening converts a nested structure into a single-level map by joining key paths (e.g. address.geo.lat → "address.geo.lat": 13.08). You do it to load JSON into flat stores like CSV/columns, index it for search, feed analytics, or simplify diffing — trading the natural hierarchy for a flat, uniform shape.

Coffee Chat Question

Concept Made Simple

What does it mean to flatten JSON and when would you do it?

🧠Mind Map Answer

Remember It Faster

Input{ a: { b: 1 } }
Output{ "a.b": 1 }
WhyCSV/columns, search, analytics, diffs
Costloses natural hierarchy

⌨️Hands-on Keyboard

Learn by Doing

javascript
function flatten(obj, prefix = "", out = {}) {
  for (const [k, v] of Object.entries(obj)) {
    const key = prefix ? `${prefix}.${k}` : k;
    if (v && typeof v === "object" && !Array.isArray(v))
      flatten(v, key, out);
    else out[key] = v;
  }
  return out;
}
console.log(flatten({ a: { b: 1 }, c: 2 }));
Output
{ "a.b": 1, "c": 2 }

🔥What If?

Think Beyond the Expected

How do you flatten arrays inside the JSON?

There's no single answer — you either index them (items.0.name, items.1.name), explode them into multiple flat rows (common in ETL), or serialize the array as a JSON string in one column. The right choice depends on whether downstream tools need to query individual elements.

😂Real World

ETL pipelines and log analytics flatten nested JSON so it fits columnar stores (BigQuery, Redshift) and is easy to query with SQL. Search indexers and config-diff tools flatten for the same reason.

🎯Interviewer's Expectation

Keywords they're listening for:

join key pathssingle-level outputfor CSV/search/analyticshandle arrays speciallyloses hierarchy

⚠️Common Mistakes

  • Ignoring arrays during flattening
  • Key collisions from a bad separator choice
  • Flattening when consumers actually need the hierarchy

Best Practices

  • Pick a clear, collision-safe path separator
  • Decide an array strategy up front (index/explode)
  • Flatten only at the boundary that needs it

🔁Follow-up Questions

  • 1How do you handle arrays when flattening?
  • 2How would you un-flatten back to nested?
  • 3What tools flatten JSON (jq, pandas.json_normalize)?

🧩Related Technologies

ETLpandas.json_normalizejqBigQuery

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: "What does it mean to flatten JSON and when would you do it?"

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?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission — at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions