How to Flatten Nested JSON
Flattening rewrites a nested document as one level of keys, joining the path with dots so that user.address.city becomes a single key. It is how JSON is loaded into a spreadsheet, a column store or an environment-variable set. It reverses cleanly unless keys already contain dots, or arrays are indexed into the key names.
JSON Flatten
Turn nested JSON into a flat map of dot-separated paths.
Nested JSON is natural to produce and awkward to consume. Anything expecting rows and columns — a CSV export, a database table, a analytics warehouse — needs one level, and flattening is the standard way to get there.
The operation is usually reversible, which is what makes it safe. The two exceptions are worth knowing before you flatten something you intend to reconstruct later.
Step by step
-
Choose the path separator
A dot is conventional and reads well. Choose something else if your keys already contain dots, because the separator has to be a character that never appears in a key name or the path becomes ambiguous.
-
Decide how arrays are handled
Arrays are usually flattened by index, so tags becomes tags.0 and tags.1. That is reversible but produces a different key set for every record, which matters if you are building spreadsheet columns from the keys.
-
Flatten and check the deepest path
Look at the longest key in the output. It shows the true depth of the document and often reveals nesting nobody knew was there — a wrapper object around the payload, for example.
-
Watch for key collisions
If a document contains both a key literally named a.b and a nested a containing b, both flatten to the same key and one silently wins. This is rare but silent, which is what makes it dangerous.
-
Unflatten to restore the structure
The reverse operation rebuilds the nesting from the paths. Round-trip a sample before relying on it: flatten, unflatten, and compare against the original to confirm nothing was lost.
Example
A two-level document and its flattened form. Note how the array becomes indexed keys.
Nested
{
"user": { "name": "Ada", "address": { "city": "London" } },
"tags": ["admin", "dev"]
}
Flattened
{
"user.name": "Ada",
"user.address.city": "London",
"tags.0": "admin",
"tags.1": "dev"
}
Frequently asked questions
What does flattening JSON mean?
Can flattening be reversed?
How are arrays flattened?
Why would I flatten JSON at all?
What happens to empty objects and arrays?
Tools used in this guide
All tools →- JSON Flatten Turn nested JSON into a flat map of dot-separated paths. In your browser
- JSON Unflatten Rebuild nested JSON from a flat map of dot-separated paths. In your browser
- JSON to CSV Converter Flatten a JSON array into spreadsheet-ready CSV. In your browser
- JSON Path Extractor Pull specific values out of a JSON document by path. In your browser
Related guides
All guides →- How to Convert JSON to SQL Turn a JSON array into INSERT statements, and the traps to avoid when you run them.
- How to Format JSON Turn minified or broken JSON into something readable, and fix it when it will not parse.
- CSV vs JSON Flat tables or nested structures — which format suits the data and the reader.
Last reviewed .