JSON Flatten / Unflatten
Flatten nested JSON into dot-path keys like user.address.city, or turn flat dot-path keys back into nested JSON.
Was this tool helpful?
Thanks for the feedback!
This tool turns nested JSON into a single-level object whose keys are paths, such as order.customer.city, and can turn those path keys back into nested JSON. Flat keys are easier to put into spreadsheet columns, translation files, environment variables and form field names. Developers and data analysts use it when one system speaks nested JSON and the other wants flat key-value pairs.
The options
Direction chooses between "Flatten: nested → dot paths" and "Unflatten: dot paths → nested". Separator sets the character placed between levels: Dot (user.name), Underscore (user_name), Slash (user/name) or Double underscore (user__name). Whatever separator you pick, array positions are always written as [0], [1] and so on.
How flattening works
The tool walks through the document. Each object key is added to the path with your separator, each array item adds its index in square brackets, and when it reaches a plain value (text, number, true, false or null) it writes one flat key. Two details keep the data safe for a round trip:
- An empty array or empty object is kept as
[]or{}under its path, so it does not vanish. - If you paste a single value with no structure, such as
42, the output is{"$": 42}.
The Flat keys counter shows how many keys were produced.
Worked example
Input:
{"order":{"id":5012,"customer":{"name":"Ravi","city":"Chennai"},
"items":[{"sku":"TEA-250","qty":2},{"sku":"MUG-01","qty":1}]}}
Flattened with the dot separator, giving 7 flat keys:
{
"order.id": 5012,
"order.customer.name": "Ravi",
"order.customer.city": "Chennai",
"order.items[0].sku": "TEA-250",
"order.items[0].qty": 2,
"order.items[1].sku": "MUG-01",
"order.items[1].qty": 1
}
With the underscore separator, {"order":{"id":5012,"tags":[],"meta":{}}} becomes order_id, order_tags holding [] and order_meta holding {}.
How unflattening works
Each key is split on the separator. A part written as [n] is read as an array index, and the tool creates an array when the next part is an index and an object otherwise. Pasting order.id, order.items[0].sku, order.items[1].sku and order.customer.city rebuilds an order object with an items array of two objects and a customer object. The Keys read counter shows how many flat keys were processed.
Unflattening needs one JSON object. Pasting an array gives the message: To unflatten, paste one object whose keys are paths like "user.address.city" or "items[0].name".
Edge cases and choosing a separator
- Only bracketed numbers become array positions.
items.0.skuunflattens to an object with a key"0", not an array. Convert such keys toitems[0].skufirst if you need an array. - If your real key names contain the separator, they get split. A key
v1.2with the dot separator turns into{"v1": {"2": ...}}. Pick a separator that never appears inside your key names. - Double underscore is a common choice for environment variables, because dots are not allowed in variable names in most shells. For example, ASP.NET Core reads
Logging__LogLevel__Defaultas nested settings. - Large integers beyond about 9 quadrillion lose precision, since the browser's JSON parser stores numbers as 64-bit floating point. Store such IDs as strings.
When flat keys are useful
Translation libraries often name strings with dotted keys such as checkout.button.pay, and translators find a flat list easier to work through than a deep tree. Spreadsheets need one column per field, so a nested record has to be flattened before it fits a row. Form builders and some databases also refer to nested fields by a dotted path. Flatten the data, edit or map it, then unflatten it with the same separator to get the original shape back.
Once the data is flat, the JSON to CSV converter can turn a list of records into spreadsheet rows. To make the output easier to read, use the JSON formatter.
Frequently asked questions
What does it mean to flatten JSON?
How are arrays shown when JSON is flattened?
Can I convert flattened JSON back to nested JSON?
Which separator should I use for environment variables?
Why does items.0.name unflatten into an object instead of an array?
What happens to empty arrays and objects when flattening?
Related guides
- Free Developer Tools That Replace a Dozen Browser Tabs Free developer tools grouped by job: format, convert, encode, generate and test. What each one does, which run on the server, and the limits to know f...
- How to Beautify CSS, HTML and XML (Online, VS Code, Prettier) Beautifying adds line breaks and indentation so minified CSS, HTML or XML is readable again. How the free formatters work, their limits, and VS Code a...
- How to Format JSON: Pretty-Print, Validate and Read Errors Paste JSON into a formatter to indent it and catch errors. The exact syntax rules, how to read an error position, and how to do it in VS Code, Python...