nested json · four strategies

JSON to CSV with nested objects — pick how it flattens

Nested JSON has no place in a spreadsheet until it is flattened. Choose a strategy, watch the rows change, and take the code if you would rather do it in a terminal.

Converter — Ready
Awaiting input
idle — paste JSON and the rows show up here
0 rows 0 columns Tab delimiter
runs entirely in your browser
nothing is uploaded, ever
no sign-up, no account
free — no paid tier, ever
Do it in code

Flattening nested JSON in Python

This is the part that trips people up: a nested object has no column of its own, so it has to be flattened into one before it can be a cell. The function below joins the parent and child names with a double underscore.

import csv
import json


def flatten(obj, prefix="", sep="__"):
    """Turn nested dicts into a single level of key/value pairs."""
    flat = {}
    for key, value in obj.items():
        name = f"{prefix}{sep}{key}" if prefix else key
        if isinstance(value, dict):
            flat.update(flatten(value, name, sep))
        elif isinstance(value, list):
            # Arrays of primitives become one cell; arrays of objects are
            # handled by the detail strategy instead.
            flat[name] = " | ".join(str(v) for v in value)
        else:
            flat[name] = value
    return flat


with open("data.json", encoding="utf-8") as f:
    rows = json.load(f)

flat_rows = [flatten(row) for row in rows]
fieldnames = list(dict.fromkeys(k for row in flat_rows for k in row))

with open("data.csv", "w", newline="", encoding="utf-8") as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames, restval="")
    writer.writeheader()
    writer.writerows(flat_rows)

Nested objects become address__city; arrays become one joined cell

Note fieldnames is built from every row, not just the first one. If the second record has a key the first one does not, that column is still created instead of silently vanishing. restval="" fills the gap for rows that do not have it.

When the nested value is an array of objects - say a list of line items - flattening is not enough. You want the detail strategy, where each array element becomes its own row and the parent values are repeated. The converter above can do all four strategies, so you can compare them on your own data.

Worked example

A worked example

One object with a nested object and an array of strings, flattened two different ways.

customer.json

[
  {
    "id": 7,
    "name": "Ada Lovelace",
    "address": { "city": "London", "postcode": "E1 6AN" },
    "skills": ["math", "pioneer"]
  }
]

customer.csv

id,name,address__city,address__postcode,skills
7,Ada Lovelace,London,E1 6AN,math | pioneer

Flatten gives one row and joins the nested keys. The converter above defaults to a tab delimiter so the file pastes straight into Excel - switch it to comma in the settings to match this example. Switch the converter to Matrix and the same input produces two rows - one per skill - because the array is expanded instead of joined. Same data, different question answered by the table.

Step by step

Pick a strategy and see the result

Four strategies are built into the converter above. They answer four different questions about the same data.

  1. Paste the nested JSON

    Objects, arrays, or both. The converter detects the shape automatically.

  2. Open the settings cog

    The Nested data dropdown holds the four strategies. The delimiter dropdown changes the output separator.

  3. Compare the strategies

    Header/detail repeats parent values per array element. Flatten joins keys. Concatenate keeps nested objects whole in one cell. Matrix expands every combination.

  4. Download

    The preview shows the exact header and row count for the strategy you picked, so you can tell the strategies apart before exporting.

Why HopConvert

Why flatten the way we do

The details that decide whether the resulting sheet is actually usable.

The parent value survives

If a child field has the same name as a parent field, the child key is qualified (items__id) so neither value is lost.

Arrays are not mangled

Arrays of primitives join with a pipe. Arrays of objects become their own rows under the header/detail strategy.

Stable column names

Nested keys join with a double underscore, which keeps them readable and safe as a spreadsheet header.

Try all four, then decide

The same input under four strategies makes the trade-off obvious before you commit to one output.

The complete guide

The full JSON to CSV guide

The guide covers flattening rules in detail, plus delimiters, quoting and how to choose a strategy for your data.

Read the JSON to CSV guide

Related tools: JSON to CSV in Python · JSON array to CSV · JSON to SQL · CSV to JSON converter

FAQ

Frequently asked questions

How nested JSON gets turned into columns, and what the four strategies each do.

How do I flatten nested JSON?
Replace each nested object with a single key that joins the parent and child names, such as address__city. Arrays of primitives become one cell joined by a delimiter. Arrays of objects need the detail strategy, where each element becomes its own row.
Which separator should I use for nested keys?
A double underscore is the common choice because it is rare in real field names, stays readable in a spreadsheet header, and survives a round trip back to JSON without ambiguity. Some teams prefer a dot, but a dot can collide with a field name that already contains one.
What happens to arrays inside an object?
It depends on the strategy. Header/detail joins them into one cell with a pipe, Concatenate keeps the surrounding object whole in a single JSON cell, and Matrix expands each element into its own row with the parent values repeated.
How do I keep both the parent and the child value?
Never let the child overwrite the parent. When both are called id, the child key is qualified with the array it came from - items__id - so both values survive into the CSV.
How many rows will nested JSON produce?
Header/detail produces one row per array element of the nested list. Matrix produces the Cartesian product of every nested array, capped at 5,000 combinations so a huge payload cannot lock the page. The preview shows the real count.

Flatten your nested JSON

Free, instant, and private — your JSON never leaves this tab.

Convert nested JSON Read the FAQ