Sheet 08 Flatten
Flatten and unflatten JSON
Turn nested JSON into one level of path keys such as address.geo.lat, as JSON or CSV, and turn path keys back into nested JSON.
{
"id": 7,
"name.first": "Ada",
"name.last": "Lovelace",
"emails[0]": "ada@example.com",
"emails[1]": "countess@example.org",
"address.city": "London",
"address.geo.lat": 51.5,
"address.geo.lng": -0.12,
"settings[\"theme.dark\"]": true,
"settings.notify": []
}
10 path keys.
How to use it
Paste nested JSON to get a flat object whose keys are paths, or switch to Unflatten and paste path keys (one object, or an array of them) to rebuild the nesting. Use output as input flips the result back for a quick round trip.
- Separator and array indexes
- Keys are joined with the separator you choose; array indexes are written
tags[0], ortags.0with the separator. - Keys that contain the separator
- A key that would be ambiguous is quoted as a JSON string in brackets:
settings["theme.dark"]. The same goes for empty keys, keys with brackets or quotes, and digits-only keys when indexes use the separator. That is what makes flatten and unflatten exact inverses. - Empty objects and arrays
- Kept as values (
"notify": []), so they survive the round trip. - CSV
- An array of objects becomes one row per item, with every path key that appears as a column; anything else becomes a single row. Cells are quoted per RFC 4180.
Questions
What if two keys collide when unflattening?
You get an error naming the key, for example when a.b needs a to be an object but another key set a to a number. Missing array positions, such as [0] and [2] without [1], are filled with null and the status lists them.
Can I unflatten a CSV file?
Unflatten reads JSON, so convert the CSV to an array of objects first, for example with the CSV to JSON converter on JSONMender, and paste the result: its path-key headers become nested objects again.
Is my JSON uploaded?
No. Flattening runs in this tab and nothing you paste leaves it. Page views are counted with Cloudflare Web Analytics, which sets no cookies and never sees your data.