JSON Flatten

FreePrivateConfigurable separator

Turn nested objects into a single level of dot-path keys, or reverse the operation.

Nested JSON
Output
Ctrl+Enter Run

Flatten Nested JSON Online

Convert a deeply nested JSON structure into a flat object with dot-notation keys. Useful for writing to spreadsheets, feeding data into flat key-value stores, simplifying Elasticsearch mappings, or debugging complex API responses. Paste your JSON above for instant results — or grab the code snippets below to flatten JSON inside your own JavaScript or Python project.

Example

Nested input
{
  "user": {
    "name": "Alice",
    "address": {
      "city": "London",
      "zip": "SW1A"
    }
  },
  "scores": [10, 20, 30]
}
Flattened output
{
  "user.name": "Alice",
  "user.address.city": "London",
  "user.address.zip": "SW1A",
  "scores.0": 10,
  "scores.1": 20,
  "scores.2": 30
}

Flatten JSON in JavaScript

function flattenJson(obj, parentKey = '', sep = '.') {
  return Object.entries(obj).reduce((acc, [key, value]) => {
    const newKey = parentKey ? `${parentKey}${sep}${key}` : key;
    if (value !== null && typeof value === 'object' && !Array.isArray(value)) {
      Object.assign(acc, flattenJson(value, newKey, sep));
    } else if (Array.isArray(value)) {
      value.forEach((item, i) => {
        const arrKey = `${newKey}${sep}${i}`;
        if (typeof item === 'object' && item !== null) {
          Object.assign(acc, flattenJson(item, arrKey, sep));
        } else {
          acc[arrKey] = item;
        }
      });
    } else {
      acc[newKey] = value;
    }
    return acc;
  }, {});
}

// Usage
const nested = { user: { name: 'Alice', address: { city: 'London' } } };
const flat = flattenJson(nested);
// { 'user.name': 'Alice', 'user.address.city': 'London' }

Flatten JSON in Python

def flatten_json(obj, parent_key='', sep='.'):
    items = []
    for k, v in obj.items():
        new_key = f"{parent_key}{sep}{k}" if parent_key else k
        if isinstance(v, dict):
            items.extend(flatten_json(v, new_key, sep).items())
        elif isinstance(v, list):
            for i, item in enumerate(v):
                arr_key = f"{new_key}{sep}{i}"
                if isinstance(item, dict):
                    items.extend(flatten_json(item, arr_key, sep).items())
                else:
                    items.append((arr_key, item))
        else:
            items.append((new_key, v))
    return dict(items)

# Usage
import json
nested = {"user": {"name": "Alice", "address": {"city": "London"}}}
flat = flatten_json(nested)
# {'user.name': 'Alice', 'user.address.city': 'London'}

When to Flatten JSON

Spreadsheet export
Flat key-value pairs map directly to CSV columns. Flatten first, then use the JSON to CSV converter for a clean spreadsheet with no nested columns.
Elasticsearch / Redis
Many key-value stores and search indexes expect flat documents. Flattening before indexing avoids nested field mapping issues.
Config comparison
Flatten two config files then diff them for a line-by-line property comparison. Works well with the JSON Diff tool for spotting deep key changes.

How to Use the Tool

  1. Paste your nested JSON into the input panel above
  2. Choose your separator (default is . for dot notation)
  3. Click Flatten to get the single-level output
  4. Copy the result or download as .json

Frequently Asked Questions

How do I flatten nested JSON in JavaScript?

You can recursively walk objects and build flat keys such as user.name or order.items[0].price, or use this page's tool instantly in the browser. For unflatten and separator options, see JSON Flatten.

Why flatten nested API responses?

Flattening makes deeply nested API data easier to display, export, search, or compare—pair with the JSON Formatter when inspecting payloads.

How are arrays handled when flattening?

Arrays are often converted using indexes in the generated keys—for example scores.0, scores.1 in the sample above.

Can I flatten JSON in Python too?

Yes. Similar recursive logic can be used in Python data workflows—see the Python snippet on this page.

What separator is best for flattened keys?

Dot notation is common, but some workflows use underscores or custom separators. JSON Flatten on this site supports dot, underscore, and slash.

Can I export flattened output to CSV?

Yes. Flattened JSON is often ideal for CSV export—use JSON to CSV next.

Related Tools