How to Flatten Nested JSON into Dot-Notation Keys
Nested JSON is hard to drop into a spreadsheet, an .env file, or a flat key-value store. This guide explains how flattening converts nested structures into dot-notation keys, when to use it, and the mistakes to avoid.
Deeply nested JSON is great for representing structure, but it is a poor fit for anything that expects flat rows or flat keys - a spreadsheet column, an environment variable, a key-value cache, or a simple diff. The moment you need to hand a document to one of those systems, you need a way to turn user.address.city from a path you'd write in code into an actual key sitting at the top level of an object.
That transformation is called flattening, and it is exactly what the JSON Flattener does: it walks every nested object and array and rewrites the whole thing as a single-level object whose keys are dot-notation paths. This guide covers how flattening works, when it earns its keep, and the pitfalls that catch people off guard the first time they try it.
What is JSON flattening?
Flattening takes a nested JSON structure - objects inside objects, arrays inside objects, objects inside arrays, any combination - and produces a single object with no nesting at all. Every leaf value (a string, number, boolean, or null) keeps its original type, but its location is now encoded directly in the key name as a dot-separated path instead of as actual nesting.
Arrays do not disappear either; each element becomes a numbered segment in the path. So roles: ["admin", "editor"] becomes two flat keys, roles.0 and roles.1, each holding one string. Nothing is summarized, dropped, or reordered - the flat object contains exactly the same information as the nested one, just addressed differently.
Nested object to dot-notation keys
A user record with a nested array flattened into flat paths.
Input
{
"user": {
"name": "Mira",
"roles": ["admin", "editor"]
}
}Output
{
"user.name": "Mira",
"user.roles.0": "admin",
"user.roles.1": "editor"
}Why flattening JSON matters
Most tools outside the JSON world do not understand nesting at all - they understand rows and columns, or flat keys and values. Flattening is the bridge that makes nested data usable in those contexts:
- Spreadsheets and CSV - a flat key like
order.items.0.skubecomes one column, so json-flatten output drops straight into a spreadsheet or CSV export. - Environment variables and config maps - systems like
.envfiles or Kubernetes ConfigMaps only accept flat key-value pairs. - Key-value stores and caches - Redis, DynamoDB, and similar stores often want a flat map rather than an object graph.
- Simple diffing and search - a flat list of paths makes it trivial to spot exactly which leaf value changed between two versions.
How JSON flattening works under the hood
The algorithm is a recursive walk. Starting from the root, the flattener visits every property. If a value is a primitive (string, number, boolean, or null), it writes the accumulated path as a key and the value as-is. If a value is an object, it appends the property name to the current path with the delimiter and recurses into it. If a value is an array, it appends the numeric index instead of a name and recurses the same way.
The delimiter itself is configurable in the JSON Flattener - a dot is the default and the most common convention, but you can switch to a slash, an underscore, or another separator if your target system already reserves dots for something else, or if your keys happen to contain literal dots.
Where does your data go?
Flattening is pure, local computation - there is no reason it needs a server round trip. The JSON Flattener runs entirely in your browser, so customer records, API tokens, and other sensitive payloads are never uploaded, logged, or stored anywhere.
Step-by-step: flatten JSON in seconds
- Open the JSON Flattener and paste the nested object or array you want to convert.
- Pick a delimiter - the default dot works for most cases, or choose a slash or underscore to match your target system.
- Flatten the document. Every leaf value gets a single-level key that encodes its full path, including array indices.
- Copy the flat result, or feed it straight into a CSV export if you need rows and columns instead of a JSON object.
There is no account, no upload step, and no size limit beyond what your browser can comfortably hold in memory.
Common use cases for flattened JSON
- Preparing a nested API response for import into a spreadsheet as ordinary columns.
- Turning a layered config object into flat, environment-variable-style keys.
- Comparing two structures at a glance by listing their flat key paths side by side.
- Loading data into a key-value store or cache that only accepts flat maps.
- Building translation or localization files where each string lives at a dotted path.
Common mistakes when flattening JSON
Mistaking array indices for object keys
A path like tags.2 means the third element of an array, not a property literally named 2. If you are hand-writing code that consumes flattened output, remember that any purely numeric path segment usually represents an array position, which matters if you later want to reconstruct the original shape.
Delimiter collisions with real key names
If one of your original keys already contains a dot - for example a field literally named first.name - flattening with a dot delimiter makes that path ambiguous with genuine nesting. Switch to a delimiter that does not appear in your data, such as a slash or a pipe, before flattening.
Forgetting there is a way back
Flattening feels like a one-way trip the first time you do it, but it does not have to be. The companion JSON Unflattener reads the same dot-notation keys and rebuilds the original nested object, so you can safely flatten for a spreadsheet edit and reconstruct the structure afterward.
Best practices for flattening JSON
- Stick with the default dot delimiter unless your data or target system genuinely requires something else, so paths stay familiar to other engineers.
- Check your source keys for existing dots or slashes before flattening, and pick a delimiter that avoids collisions.
- Use flattened output for comparison when you want to see exactly which leaf values differ between two versions of a document.
- Pair flattening with the JSON Tree Viewer first if the source structure is unfamiliar - understanding the shape makes the flat key paths much easier to read afterward.
- Keep the original nested JSON around, or remember the delimiter you used, so you can unflatten cleanly later.
Related tools
Related guides
Flat key-value data from a spreadsheet or key-value store is hard to use as-is. This guide explains how JSON unflattening rebuilds the original nested objects and arrays, safely, in your browser.
Spreadsheets don't understand nested JSON. This guide explains how a JSON array becomes clean, aligned CSV rows, and how to avoid the mistakes that corrupt spreadsheet imports.
Raw JSON text hides its own shape. This guide shows how a collapsible tree view turns an unfamiliar payload into something you can scan, search, and navigate in seconds.
A JSON payload can look simple and still hide bloated arrays, dangerous nesting, or dozens of duplicate keys. This guide shows how to profile any document's size, depth, and type makeup before it becomes a problem.