How to use the json flatten
- Paste your nested JSON into the Nested JSON text area.
- Choose a key separator such as dot, slash or underscore.
- The flattened result appears immediately below as you type.
- Copy the output to use in configuration files, databases or APIs.
How flattening works
To json flatten a nested structure means to recursively walk every property and build a single string key that records the path from the root. For objects, the child key is appended to the parent key with a separator. For arrays, the numeric index is wrapped in brackets.
Consider this input: {"user": {"name": "Ada", "tags": ["admin", "dev"]}}. The algorithm visits user, then name inside it, producing the key user.name with value "Ada". It then visits tags, an array, and creates user.tags[0] for "admin" and user.tags[1] for "dev". The final flat object has three top-level keys and no nesting at all.
Separator choices
The separator determines how object levels are joined in the resulting key. Different ecosystems prefer different conventions:
| Separator | Example key | Common usage |
|---|---|---|
Dot (.) | user.address.city | MongoDB queries, JavaScript libraries, most APIs |
Slash (/) | user/address/city | REST paths, Firebase Realtime Database |
Underscore (_) | user_address_city | Environment variables, SQL column names |
Array indices always use brackets regardless of the chosen separator, so items[0].id stays readable even with an underscore separator: items[0]_id.
Practical use cases
Flattening JSON is useful whenever a system expects a simple key-value map rather than a tree. Configuration management tools often require flat environment variables, which you can produce by flattening a config file and converting keys to uppercase. If you need to convert your data further, tools like JSON to ENV or JSON to properties handle those specific formats.
Databases that do not support nested documents benefit from a flat schema. When migrating data from a document store to a relational table, flattening helps map each leaf value to a column. You might also pair this with JSON formatter to pretty-print the result, or JSON viewer to inspect the original tree side by side.
In front-end development, form libraries sometimes bind inputs to flat field names. Flattening a deeply nested API response makes it straightforward to populate such forms without writing manual mapping code.
Edge cases and limits
A few edge cases deserve attention when you flatten JSON:
- Empty objects and arrays. An empty object
{}or empty array[]is kept as a value at its path, for example"tags": [], so no key disappears. If two different paths would produce the same flat key (for example a key literally nameda.bnext to a nestedacontainingb), the tool reports an error instead of overwriting one of them. - Primitive roots. If the root value is a string, number, boolean or null, it is already flat. The tool returns it as-is.
- Keys containing the separator. If a source key literally contains a dot and you use dot as the separator, the flattened key becomes ambiguous. This is an inherent limitation of delimiter-based flattening. Consider switching to slash or underscore if your data includes dots in key names.
- Unicode. All string values and keys preserve Unicode characters exactly, including emoji and non-Latin scripts.
- Very deep nesting. Browsers limit call-stack depth. Extremely deep structures (thousands of levels) may cause a stack overflow. Typical API responses rarely exceed ten levels.
Flattening versus other transformations
Flattening reduces nesting but keeps the data in JSON format. Other transformations change the format entirely. For example, you might convert JSON to XML for SOAP services, or turn tabular data into a visual layout with JSON to HTML table or JSON to markdown table.
If you need to generate strongly typed models from your JSON, consider dedicated converters such as JSON to Java class, JSON to C# class, JSON to Go struct or JSON to Python class. Each serves a different stage of the development pipeline, while flattening focuses purely on structural simplification.
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple nested object | Nested JSON: {"user":{"name":"Ada","age":30}}; Key separator: . | { / "user.name": "Ada", / "user.age": 30 / } |
| Object with arrays | Nested JSON: {"data":[{"id":1},{"id":2}]}; Key separator: . | { / "data[0].id": 1, / "data[1].id": 2 / } |
| Using slash separator | Nested JSON: {"a":{"b":{"c":true}}}; Key separator: / | { / "a/b/c": true / } |
FAQ
What does it mean to json flatten an object?
It means converting a nested JSON structure into a single-level object where each key is the full path to the value, typically joined by dots. Arrays use bracket notation for their indices.
Are arrays handled differently from objects?
Yes. Object children are joined with the chosen separator, while array elements are appended with their numeric index in brackets, such as users[0].name.
Is my data sent to a server?
No. Parsing and flattening happen entirely in your browser using JavaScript. Nothing leaves your device.
What happens if I paste invalid JSON?
The tool displays a clear error message indicating where the parser failed, so you can correct the syntax before trying again.
Can I reverse the flattening process?
Yes. The reverse operation is called unflattening, which reconstructs the nested tree from dot-notation keys. A separate JSON unflatten tool handles that.
Which separator should I choose?
Use dot notation for most JavaScript and MongoDB workflows. Use slashes for URL-like paths or Firebase, and underscores when targeting environment variables or SQL columns.