FormatterHQ

JSON Flatten: Nested JSON to Dot Notation

Turn deeply nested JSON into one flat object whose keys are full paths such as user.address.city or items[0].sku. Choose the separator and how arrays are written.

Input JSON
Output JSON
Ready.

How to use the JSON Flatten

  1. Paste a JSON object or array into the input editor.
  2. Set the Key separator (a dot by default; use _, / or __ if you prefer).
  3. Pick an Arrays style: index as key (a.0.b), brackets (a[0].b) or keep arrays as values.
  4. Click Flatten and copy or download the flat JSON.

Flatten nested JSON with the JSON flatten tool

JSON flatten converts a nested document into a single-level object. Every leaf value keeps its data type, and its key becomes the full path to that value, joined by a separator. {"user": {"address": {"city": "Paris"}}} becomes {"user.address.city": "Paris"}.

Flat keys are easier to diff, grep, sort, store in a key-value database or load into a spreadsheet column. They are also the format many translation files, feature-flag services and logging pipelines expect.

What does flattening JSON mean?

A JSON document is a tree. Flattening walks the tree and writes one entry for each leaf (a string, number, boolean or null), using the chain of object keys and array indexes as the new key. Nothing is lost: with the same separator, JSON unflatten rebuilds the original structure.

Example

Input:

{
  "user": {
    "id": 7,
    "address": { "city": "Paris" },
    "tags": ["admin", "beta"]
  },
  "meta": {}
}

Output with the default settings:

{
  "user.id": 7,
  "user.address.city": "Paris",
  "user.tags.0": "admin",
  "user.tags.1": "beta",
  "meta": {}
}

Notice that the empty object meta is kept as {} rather than silently dropped, so it survives a round trip.

Options explained

Key separator

The string placed between path segments. A dot matches lodash _.get, MongoDB and most i18n libraries. Use __ when the flat keys will become environment variables, or / to make them look like file paths.

Arrays

  • Index as key (user.tags.0): arrays are treated like objects whose keys are numbers.
  • Brackets (user.tags[0]): the index is written in square brackets, which reads like JavaScript and is what Spring and many form libraries use.
  • Keep arrays as values: arrays are not expanded. With the _ separator, the example above gives "user_id": 7 and "user_tags": ["admin", "beta"]. Pick this when lists should stay lists, for example tag columns.

Indent

Formats the output with 2 spaces, 4 spaces or tabs.

Edge cases

  • Keys that already contain the separator. A key like "v1.2" produces api.v1.2, which cannot be told apart from nested keys when you unflatten. Choose a separator that does not occur in your keys.
  • Empty arrays and objects are written as [] and {} under their path instead of disappearing.
  • A top-level array is allowed; its keys start with the index, such as 0.name or [0].name.
  • A bare value such as 42 or "text" is rejected, because there is nothing to flatten.
  • Large numbers pass through JavaScript's JSON parser, so integers above 253 lose precision just as they would in any browser app.

When to flatten JSON

  • Comparing two configs line by line: flatten both, then diff them with JSON diff for a list of changed paths.
  • Exporting nested API responses to CSV, where every column must be a single key.
  • Building i18n catalogs where translators work with keys like checkout.button.pay.
  • Writing search or analytics fields that do not support nesting.

If you only need to find the path of one value, the JSON path finder is quicker: click the value and copy its path.

Privacy

Flattening runs in your browser. Your JSON is not uploaded anywhere, so it is safe to paste production configs or API responses.

JSON flatten function in Python

A JSON flatten function in Python is a short recursive walk over the parsed data; the json module does the parsing and nothing else is needed. Dictionaries add their keys to the path and lists add their indexes. The detail most versions get wrong is empty containers: a loop over {} or [] adds nothing, so the key silently disappears. The and value checks below send empty containers to the leaf branch, which keeps them.

import json

def flatten(value, prefix="", sep="."):
    if isinstance(value, dict) and value:
        items = value.items()
    elif isinstance(value, list) and value:
        items = enumerate(value)
    else:
        return {prefix: value}
    flat = {}
    for key, child in items:
        path = f"{prefix}{sep}{key}" if prefix else str(key)
        flat.update(flatten(child, path, sep))
    return flat

text = '{"user": {"id": 7, "address": {"city": "Paris"}, "tags": ["admin", "beta"]}, "meta": {}}'
print(json.dumps(flatten(json.loads(text)), indent=2))

The output is identical to the default result of the tool above for the same document, and JSON unflatten turns it back into the nested form.

Frequently asked questions

Can I get the nested JSON back after flattening?
Yes, as long as your keys do not contain the separator. Paste the flat object into JSON unflatten with the same separator and it rebuilds the objects and arrays.
How do I flatten JSON with underscores instead of dots?
Type _ (or any other string, such as __ or /) into the Key separator field. The tool joins every path segment with exactly that string.
Why is my array written as items.0 instead of items[0]?
The default Arrays setting treats indexes as ordinary keys. Switch Arrays to Brackets to get items[0].sku style keys, or to Keep arrays as values to leave lists unexpanded.
What happens to empty objects and arrays?
They are preserved. An empty object becomes "path": {} and an empty array becomes "path": [], so the structure is not lost when you flatten and unflatten again.
Are value types preserved?
Yes. Numbers stay numbers, booleans stay booleans and null stays null. Only the keys change; values are copied as they are.

Last updated October 3, 2026

Related tools

JSON Formatter Format, beautify and validate JSON online. Pretty print minified JSON with 2, 3, 4 space or tab indentation and pinpoint syntax errors. JSON Merge Patch (RFC 7396) Apply an RFC 7396 JSON Merge Patch to a document, or create the merge patch that turns one JSON document into another. JSON Minify Minify JSON by removing whitespace and line breaks. Shrink payloads for APIs, storage and embedding, and see the bytes saved. JSON Multiline String Converter Convert multi-line text into a valid JSON string with \n escapes, a JSON array of lines, or concatenated string literals. JSON Navigator Navigate JSON by path. Browse or filter the tree and click any key to see and copy the full path to that value. JSON Parser Parse JSON online: errors with line and column, the parsed values with their types, and the pitfalls of big integers and duplicate keys. JSON Patch Tester (RFC 6902) Apply RFC 6902 JSON Patch operations (add, remove, replace, move, copy, test) to a document and see exactly which step fails. JSON Patch Generator (Diff to Patch) Compare two JSON documents and generate the RFC 6902 JSON Patch that transforms the first into the second, with optional test ops. JSON Path Finder Click any value in a JSON tree to get its path as JSONPath, JavaScript, Python, PHP, jq or JSON Pointer — or list every path. JSON Pretty Print Pretty print JSON online with a 4-space default, 2 or 3 spaces or tabs, and see the matching JavaScript, Python, PHP and jq commands. JSON Reader Open a .json file in your browser and read it as a collapsible tree, a sortable table or a graph, with a filter to find any value. JSON Schema to Sample JSON Generate realistic fake JSON that conforms to a JSON Schema — formats, enums, ranges and required fields respected.