Flattening JSON β Interview Questions
Reviewed by Gurusankar M.
β‘ 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.