JSON Flattener

Turn a nested object into one level of path-style keys, or turn flat keys back into the nested structure. Empty objects and lists are kept, and clashes between keys are reported.

Direction

Worked examples

Flatten

{"user": {"name": "Ann", "emails": ["[email protected]", "[email protected]"]}}

{ "user.name": "Ann", "user.emails[0]": "[email protected]", "user.emails[1]": "[email protected]" }

Each value is at a path.

About JSON Flattener

Flat key and value pairs are what spreadsheets, form fields, translation files, environment variables and many search indexes want, while code prefers nested objects. Flattening joins the path to each value with a separator, so {"user": {"name": "Ann"}} becomes {"user.name": "Ann"}. Unflattening reverses it and rebuilds the nesting.

Lists can be written with brackets, as in emails[0], or with plain numbers, as in emails.0, and you can change the separator to a slash, an underscore or anything else. Empty objects and empty lists are kept as values, so nothing disappears, and unflattening reports a clash, for example when one key says a is a value and another says a.b exists. The round trip gives you back the original structure, and mistakes in the JSON are reported with a line and column.

How to use JSON Flattener

  1. Choose Flatten or Unflatten.
  2. Paste the JSON.
  3. Set the separator and how list positions are written.
  4. Copy the result.

Common uses

  • Preparing nested data for a spreadsheet or CSV
  • Building translation files with path keys
  • Mapping nested settings to flat environment names
  • Comparing two documents by their paths

Frequently asked questions

What happens to empty objects and lists?

They are kept as values, {} and [], at their path, so unflattening restores them.

What if a key contains the separator?

Unflattening cannot tell where one key ends and the next begins, so pick a separator that does not appear in your keys.

How are list items written?

By default as name[0], name[1]. The other style writes name.0, name.1. Use the second for tools that cannot handle square brackets.

What is a clash?

Keys that cannot both be true, for example a with a plain value and a.b with another. Unflattening stops and names the key.

Does flattening change values?

No. Values are copied as they are, including numbers, text, true, false and null. Only the keys change, and each new key is the full path to its value from the top of the document.