Flatten JSON / Unflatten JSON
Flatten nested JSON objects into dot-path keys, or unflatten dot paths back into nested objects and arrays.
Flatten nested JSON objects into dot-path keys, or unflatten dot paths back into nested objects and arrays.
Flatten JSON converts nested objects into one flat object whose keys describe each path, such as user.address.city. Flat keys are easier to search, map to spreadsheet columns, store as environment-style settings or compare line by line. Choose Unflatten JSON to rebuild the nested structure from those paths.
The conversion runs entirely in your browser and keeps strings, numbers, booleans, null and empty containers exactly as they were. The result is pretty-printed with two-space indentation, ready to copy.
{"user":{"name":"Ada","address":{"city":"London","zip":"N1"}},"active":true,"note":null,"meta":{}}Flatten JSON output:
{
"user.name": "Ada",
"user.address.city": "London",
"user.address.zip": "N1",
"active": true,
"note": null,
"meta": {}
}Every leaf value gets a full path. null is kept, and the empty meta object stays as {} so unflattening can restore it.
Numeric segments create arrays when the parent does not exist yet:
{"order.id":17,"order.items.0.sku":"A1","order.items.0.qty":2,"order.items.1.sku":"B2","order.items.1.qty":1}Unflatten output:
{
"order": {
"id": 17,
"items": [
{
"sku": "A1",
"qty": 2
},
{
"sku": "B2",
"qty": 1
}
]
}
}When unflattening, each segment is decoded the way JSON Pointer does it: ~1 becomes / and ~0 becomes ~. That lets you rebuild OpenAPI-style path keys:
{"paths.~1api~1v1.method":"GET","paths.~1api~1v1.auth":"token"}becomes
{
"paths": {
"/api/v1": {
"method": "GET",
"auth": "token"
}
}
}
{"tags":["a","b"]} flattens to tags.0 and tags.1, and nested arrays such as [[0,0]] give pts.0.0, pts.0.1. Unflatten turns numeric segments back into arrays, so a flatten followed by an unflatten returns the original document."a.b" and a nested path a → b produce the same flat key, so one value overwrites the other. Rename such keys first."a": 2 and "a.b": 1, the scalar wins and the nested value is dropped without an error.x.0 and x.3 produce a four-item array; the gaps serialise as null.tags[0] are treated as plain key names, not array indexes.Flattening is the right step when a target system wants one key per value: a mapping file, a translation catalogue, a feature-flag store or a key-value database. If you want spreadsheet rows, JSON to CSV and JSON to Table build columns from nested records directly. To read a single deep value without changing the document, query it with the JSONPath tester. To see exactly which paths changed between two versions of a config, the JSON diff checker reports differences by path.
For a repeatable pipeline, the same dot-path convention takes a few lines of Python. This version also walks lists, writing indexes as path segments, so its output unflattens correctly with the tool above:
def flatten(obj, prefix=""):
out = {}
items = obj.items() if isinstance(obj, dict) else enumerate(obj)
for key, value in items:
path = f"{prefix}.{key}" if prefix else str(key)
if isinstance(value, (dict, list)) and value:
out.update(flatten(value, path))
else:
out[path] = value
return outIn pandas, pd.json_normalize(records, sep=".") flattens nested dictionaries into column names in the same style, though it leaves lists as cell values. Whichever route you use, unflatten a sample of the output and diff it against the source before trusting a large batch.
Flattening turns a nested structure into a single-level object whose keys are paths. {"user":{"name":"Ada"}} becomes {"user.name":"Ada"}. Unflattening is the reverse.
A dot. The separator is not configurable, and keys that already contain a dot are written unchanged, so they cannot be told apart from nesting when you unflatten later.
Use numeric path segments. items.0.sku and items.1.sku rebuild an items array with two objects. Missing indexes become null.
No. Only dot-separated segments are recognised, so user[0].name becomes a key literally named user[0]. Rewrite brackets as user.0.name first.
Yes. Strings, numbers, booleans and null pass through untouched, and empty objects and arrays are kept as values under their own path.
No. Flattening and unflattening run in your browser and the data never leaves the page.