FormatterHQ

Data formats

How to Convert JSON to CSV (Flat and Nested Data)

Convert JSON to CSV the right way — flat data in one step, nested objects and arrays without dropped columns, plus the type and encoding pitfalls to avoid.

Published 5 min read

Converting JSON to CSV sounds trivial — and it is, right up until the JSON has nested objects or arrays. Then columns silently go missing, numbers change, and rows misalign. This guide covers the easy case, the nested case, and the type and encoding traps that bite people, so your CSV actually matches your data.

Before converting anything you did not produce, validate and eyeball it in the JSON formatter — a converter can only be as correct as the input.

The easy case: an array of flat objects

When your JSON is an array of objects that all share the same simple keys, conversion is one step — each object is a row, each key a column:

[
  { "id": 1, "name": "Ada",  "active": true },
  { "id": 2, "name": "Alan", "active": false }
]

Paste that into the JSON to CSV converter and you get a clean two-row table with id,name,active headers. Everything runs in your browser, so the data never leaves your device. If different objects have different keys, a good converter builds the column set from the union of all keys and leaves missing cells blank.

The hard case: nested objects and arrays

CSV is a flat grid with one value per cell, so nested structures have nowhere to go. Take this record:

{
  "id": 1,
  "name": "Ada",
  "address": { "city": "London", "zip": "EC1" },
  "roles": ["admin", "author"]
}

There is no single “address” or “roles” cell that preserves the structure. You have two sensible choices:

  • Flatten objects to dotted columns. address.city and address.zip become their own columns. The JSON flatten tool does exactly this — flatten first, then convert, and nothing is dropped.
  • Decide what arrays mean. An array like roles either joins into one cell ("admin, author") or explodes into one row per value (duplicating the other columns). Which is right depends on whether downstream code treats the CSV as one row per person or one row per role.

The practical workflow: run the document through flatten, check that the generated columns are what you expect, then convert the flattened result to CSV.

Type loss and the pitfalls that change your data

CSV has no types — every cell is text — so conversions can quietly alter values:

  • Booleans and null become the strings true/false and an empty cell. Agree on a convention before you import.
  • Long numeric IDs can be rounded or shown in scientific notation by a spreadsheet. Keep IDs as strings in the JSON so they survive.
  • Leading zeros (zip codes, product codes) are stripped when a tool guesses the column is numeric.
  • Commas, quotes and newlines inside a value must be quoted and escaped. A correct converter handles this (RFC 4180); a naive join(",") does not.

Encoding and delimiters

Save CSV as UTF-8 so accents and non-Latin text survive. Note that some spreadsheet programs assume a regional delimiter (a semicolon rather than a comma) when you double-click a file — if columns all land in one cell, import the file explicitly and pick the comma delimiter. To sidestep CSV's encoding and delimiter quirks entirely, convert straight to a spreadsheet with the JSON to Excel tool instead.

Doing it in code

For a one-off, the online converter is fastest. For a repeatable pipeline, do it in code — in Python, pandas.json_normalize() flattens nested records before to_csv(); in JavaScript, libraries like json2csv accept a field list so you control the columns. Either way, parse and inspect the JSON first (see parsing JSON in Python) so you know exactly what you are flattening. Pin the column order explicitly in a pipeline: if you let it default to the keys of the first record, a later record with extra or missing fields can shift every column and quietly corrupt the output.

A worked example, start to finish

Suppose an export gives you a list of users, each with a nested address and a list of roles. The reliable path is three steps. First, format the file so you can see the structure and confirm it is an array of objects. Second, flatten it so address.city and address.zip become their own columns and you decide whether roles joins into one cell or explodes into multiple rows. Third, run the flattened result through JSON to CSV and open it. Doing it in that order — inspect, flatten, convert — means you catch a surprise field or a type problem before it reaches the spreadsheet, instead of debugging a mangled CSV afterwards.

When your JSON is an object, not an array

CSV needs rows, so a converter expects an array at the top level. Real payloads often wrap the array in an object instead:

{ "page": 1, "users": [ { "id": 1 }, { "id": 2 } ] }

Here the rows you want are under users, not at the root. Point the converter (or your code) at that array — in Python, pandas.json_normalize(data, record_path="users") — or extract it first with a JSONPath expression like $.users[*]. If the object is really a map of id → record ({"1": {...}, "2": {...}}), decide whether the key should become a column before you convert, or those identifiers are lost.

When CSV is the wrong choice

CSV is perfect for flat, tabular data headed into a spreadsheet or a database import. It is a poor fit when your data is deeply nested, when fields contain their own structured data, or when types matter and must round-trip exactly. In those cases keep the JSON, or convert to a format that preserves structure — Excel for a richer spreadsheet, or leave it as JSON for an API. Flatten to CSV only when the destination genuinely wants a grid.

Key takeaways

  • Flat array of objects → convert directly with JSON to CSV.
  • Nested data → flatten first so objects become dotted columns and nothing is dropped.
  • Decide whether arrays join into a cell or explode into rows.
  • Watch type loss — keep IDs and zip codes as strings; save as UTF-8.
  • For a cleaner result, convert to Excel instead of CSV.
  • JSON
  • CSV
  • Conversion
  • Data

Frequently asked questions

How do I convert JSON to CSV?
If your JSON is an array of flat objects, paste it into the JSON to CSV converter — each object becomes a row and each key a column. Nested data needs flattening first (see below).
Why are some columns missing when I convert JSON to CSV?
CSV is a flat grid, so nested objects and arrays do not map onto a single row. A converter either flattens them into dotted columns or drops them. Flatten the JSON first with the JSON flatten tool so every leaf becomes its own column.
Can I convert nested JSON to CSV?
Yes, but you must decide how nesting becomes columns. Objects flatten to parent.child columns; arrays either expand to one row each or join into a single cell. Flatten first, review the columns, then convert.
How do I convert JSON to Excel instead of CSV?
Use the JSON to Excel tool, which produces an .xlsx file. It avoids CSV's encoding and delimiter pitfalls and preserves more formatting.
Why do my numbers or booleans look wrong in the CSV?
CSV has no types — everything is text. Leading zeros, long IDs and booleans can be reinterpreted by a spreadsheet. Keep IDs as strings in the source JSON, and open the CSV with an explicit import step rather than a double-click if a column matters.

Tools used in this guide