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.
On this page
Concept Made Simple
Coffee Chat Question
“What does it mean to flatten JSON and when would you do it?”
Remember It Faster
Mind Map Answer
- Input
- { a: { b: 1 } }
- Output
- { "a.b": 1 }
- Why
- CSV/columns, search, analytics, diffs
- Cost
- loses natural hierarchy
Learn by Doing
Hands-on Keyboard
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 }Think Beyond the Expected
What If?
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 paths
- single-level output
- for CSV/search/analytics
- handle arrays specially
- loses 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
- How do you handle arrays when flattening?
- How would you un-flatten back to nested?
- What tools flatten JSON (jq, pandas.json_normalize)?
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?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair