Boneyard Tools

JSON Flatten and Unflatten

Flatten deeply nested JSON into a single level of dot-notation keys, where nested objects join with a dot and array items use their index, so a.b and list.0 describe the whole path. Switch to unflatten to rebuild the original objects and arrays from those flat keys. The output is pretty-printed with a two-space indent, and you can set any delimiter you like in place of the dot.

How to flatten JSON

  1. Click Flatten to collapse nested JSON, or Unflatten to rebuild it.
  2. Optionally change the Delimiter field from the default dot to another character.
  3. Paste your JSON into the input box labelled Nested JSON or Flat JSON.
  4. Read the pretty-printed result; an error appears if the JSON cannot be parsed.
  5. Click Copy to take the result with you.

Examples

Flatten a nested object and array

{ "a": { "b": 1 }, "list": [10, 20] }
{
  "a.b": 1,
  "list.0": 10,
  "list.1": 20
}

Unflatten dot keys back into structure

{ "a.b": 1, "list.0": 10, "list.1": 20 }
{
  "a": {
    "b": 1
  },
  "list": [
    10,
    20
  ]
}

Flatten with a slash delimiter

{ "user": { "name": "Ada", "roles": ["dev"] } }
{
  "user/name": "Ada",
  "user/roles/0": "dev"
}

Frequently asked questions

How are arrays represented when flattening?

Array items use their numeric index as a path segment, so the first item of list becomes list.0 and the second becomes list.1. When you unflatten, any segment made only of digits is turned back into an array position.

Can I change the delimiter?

Yes. The Delimiter field defaults to a dot but accepts up to three characters, such as a slash or an underscore. Use the same delimiter for unflatten that you used for flatten, or the paths will not split correctly.

Does flatten then unflatten return the original JSON?

For ordinary JSON, yes. The two operations are inverses, so flattening and then unflattening reproduces the same objects, arrays and leaf values.

What counts as a leaf value?

Strings, numbers, booleans and null are leaves and become the values of the flat keys. Only objects and arrays are descended into, so a null is preserved as null rather than expanded.

What happens to empty objects or arrays?

An empty object or empty array holds no leaf values, so it produces no keys at all when flattened. There is nothing to represent, so it simply disappears from the flat result.

What if a real key contains the delimiter?

A dot inside an actual key is indistinguishable from a path separator, so a key like a.b will be read as two segments on unflatten. If your keys contain dots, choose a delimiter that does not appear in them.

Why does unflatten sometimes create an array with gaps?

Container type is inferred from the digits in a segment, so keys like list.0 and list.2 build an array whose index 1 is empty. Provide every index if you need a dense array with no holes.

Does the tool require an object at the top level?

Flatten accepts any JSON, including a bare array or a single value. Unflatten expects a flat object of key to value pairs, and it warns you if you give it something else.

Is my JSON uploaded anywhere?

No. Everything is parsed and transformed in your browser, so your JSON stays on your device and nothing is sent to a server.

Learn more

Related tools