Resell services with the EthicalSMM API

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.sku unflattens to an object with a key "0", not an array. Convert such keys to items[0].sku first if you need an array.
  • If your real key names contain the separator, they get split. A key v1.2 with 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__Default as 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?
Flattening turns a nested JSON document into one level of keys, where each key is the full path to a value. {"user":{"name":"A"}} becomes {"user.name":"A"}.
How are arrays shown when JSON is flattened?
Each array item gets its index in square brackets, starting from zero. An order with two items gives keys like order.items[0].sku and order.items[1].sku.
Can I convert flattened JSON back to nested JSON?
Yes. Choose Unflatten in the Direction box and use the same separator you flattened with. The tool rebuilds the nested objects and arrays from the path keys.
Which separator should I use for environment variables?
Double underscore is the usual choice, because most shells do not allow dots in variable names. A setting like database.host becomes database__host.
Why does items.0.name unflatten into an object instead of an array?
The tool only treats numbers in square brackets as array positions. Write the key as items[0].name if you want an array.
What happens to empty arrays and objects when flattening?
They are kept as [] or {} under their path, so unflattening gives you back the same structure.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.