Flattening JSON — Interview Questions
⚡ 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
⌨️Hands-on Keyboard
Learn by Doing
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 }));{ "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:
⚠️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
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.
Was this answer helpful?
⭐ Featured Products
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.