JSON Flattener / Unflattener
Flatten nested JSON objects into dotted paths.
Overview
The JSON flattener collapses a nested JSON object into a flat map keyed by dotted paths, and the unflattener reverses the process. A document like {"a":{"b":{"c":1}}} becomes {"a.b.c":1} and vice versa.
Both directions are useful: flattened JSON is easier to diff line by line, easier to import into a CSV column, and the natural shape for environment variables. Developers building config tooling, comparing API responses, or generating .env files from a structured source use a json flatten tool to bridge between hierarchical and flat representations.
How it works
Flattening walks the document depth-first. At each leaf (a scalar value), the accumulated path becomes the key. Path separators default to . but can be changed to _, :, /, or any custom string for systems that reserve dots in identifiers. Arrays are addressed by index (items.0.name) or by bracket notation (items[0].name) depending on the chosen format.
Unflattening is the inverse: split each key by the separator, walk the path, and create the intermediate objects (or arrays, if the path segment is an integer). Mixed integer/string siblings under the same parent disambiguate to objects by default; toggle the option to force array creation when all siblings are sequential integers.
Examples
Input:
{
"user": {
"name": "Alice",
"tags": ["admin", "ops"]
}
}
Flattened (dot):
{
"user.name": "Alice",
"user.tags.0": "admin",
"user.tags.1": "ops"
}
Unflatten (with separator "/"):
Input: {"a/b/c": 1, "a/b/d": 2}
Output: {"a":{"b":{"c":1,"d":2}}}
FAQ
What if a key already contains a dot?
Dots inside keys are a problem with the default separator. Either pick a different separator (/, :) or enable the escape mode that wraps such keys in brackets: [user.name] survives the round trip intact.
Is the round trip lossless?
For pure trees of objects, arrays, and scalars, yes. Null values, empty objects, and empty arrays are preserved. The only edge case is the bracket vs. dot decision for arrays, which the chosen format controls.
Can I use this to build env-var style configs?
Yes — flatten with _ separator and uppercase the result, and you have an .env-friendly shape like USER_NAME=Alice. Type coercion to strings happens automatically for scalar values.