Skip to content
Allin

How to Convert JSON to CSV: Flattening Arrays of Objects into Rows and Columns

Published 6/18/2025 · 4 min read · Developer tools

Daniel Okonkwo

Daniel OkonkwoFront-end developer and tech writer at Allin

Web performance · File formats

Checked against 2 sources

View profile
In short

To convert JSON to CSV, start from an array of objects. Collect the union of all keys to form the header row, then write one line per object, reading each field in header order. Escape any value that contains a comma, double quote, or line break by wrapping it in double quotes and doubling internal quotes. Nested objects and arrays must first be flattened into dot-notation columns such as address.city.

A practical guide to turning a JSON array of objects into a clean CSV file, including how to flatten nested fields and handle the tricky edge cases.

Why the shape of your JSON matters

CSV is a flat, two-dimensional format: rows and columns, nothing more. JSON is a tree that can nest objects inside objects and arrays inside arrays. The clean case for conversion is a top-level array of objects, where each object becomes a row and each key becomes a column. Anything deeper has to be reshaped before it can fit into a grid.

Because different objects may carry different keys, the header row should be the union of every key seen, not just the keys of the first object. If you only read the first record, later columns silently vanish and the file becomes misaligned.

Flattening nested objects and arrays

The standard trick is dot notation. An object like {"address":{"city":"Lagos"}} becomes a column named address.city holding the value Lagos. Nesting can go as deep as you like: user.address.city is perfectly valid. Arrays are indexed by position, so tags containing ["api","web"] becomes two columns, tags.0 and tags.1.

Flattening works well when arrays are short and fixed in length. When an array can grow, indexed columns explode: a records with fifty tags produces fifty columns. In that case it is often cleaner to keep the array as a single joined string, such as "api;web;csv", or to split the data into a second related table.

Edge cases that break naive converters

The most common failure is quoting. If a value contains the delimiter (usually a comma), a double quote, or a line break, it must be wrapped in double quotes, and any double quote inside it must be doubled. So the value She said "hi" becomes "She said ""hi""". Skipping this corrupts every downstream row.

Other traps include null versus empty string (both usually render as an empty cell), booleans and numbers that a spreadsheet may reinterpret (a leading-zero code like 007 can lose its zeros), and encoding. Always write UTF-8, and consider a leading byte order mark if the file must open cleanly in older spreadsheet apps.

JSON to CSV converterTurn a JSON array of objects into comma-separated values with a header row. Takes a whole file, so a long log does not have to go through the clipboard.Try the tool

Frequently asked questions

What if my JSON is a single object, not an array?
Wrap it in an array so it becomes a one-row CSV. If you instead want each key on its own line, transpose it into a two-column key,value layout.
Which delimiter should I use, comma or semicolon?
The comma is the default in the CSV standard. Some regional spreadsheet setups expect a semicolon; if your values already contain commas, a semicolon or tab can reduce quoting.
How do I keep leading zeros from disappearing?
The zeros are in the CSV file; a spreadsheet drops them when it treats the column as a number. Import the column as text, or the spreadsheet will reformat it on open.
Can I round-trip the CSV back into JSON?
Partly. Flat columns convert back cleanly, but dot-notation keys need re-nesting and CSV loses type information, so numbers and booleans come back as strings unless you re-parse them.

Articles you may find interesting

All guides
ExplainercamelCase vs snake_case: A Guide to Naming Conventions in CodecamelCase, snake_case, PascalCase, and kebab-case explained: what each looks like, where it is the convention, and how to choose one consistently.ExplainerCSV to JSON: The Five Cases That Break Every ConverterQuoted delimiters, embedded newlines, ambiguous types, duplicate headers and encoding. Each one was run through the converter and the exact output is printed here — including the two cases it does not rescue.ExplainerXML to JSON: Attributes, Repetition, and the Single-Element Array TrapTwo documents that differ only in how many children exist produce two different JSON shapes, and no converter can tell them apart without a schema. Plus what this one really does with attributes, mixed content and whitespace — and the one thing it still cannot record.ComparisonJSON vs XML: What's the Difference?JSON and XML both store structured data as text, but they trade off differently. Here's how each looks, where each wins, and how to choose.ExplainerJSON to CSV When the Structure Is Nested: Why There Is No Right AnswerThe same two orders come out as five columns from one converter and ten from another, and neither is wrong. Dotted paths, arrays of scalars, arrays of objects and records with different keys — four decisions, made for you, usually silently.ExplainerEscaping a String for JSON: Three Characters Are Mandatory, and One Is a TrapRFC 8259 requires exactly three things to be escaped inside a JSON string. Everything else is optional. The one that actually breaks pipelines is a lone surrogate — legal in JSON text, impossible in UTF-8, and silently replaced the moment your data is written out.

Related tools

Sources

Spotted a mistake in this article?