JSON to CSV Converter

Turn a JSON array into CSV you can open in a spreadsheet. The keys of every object together make up the header row. Any value that contains the delimiter, a double quote or a line break is wrapped in quotes with its inner quotes doubled, as RFC 4180 specifies.

INPUT JSON
CSV OUTPUT

1 JSON to CSV example

Input JSON

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[
    {
        "id": 1,
        "name": "John Doe",
        "email": "john@example.com",
        "roles": ["admin", "editor"],
        "active": true
    },
    {
        "id": 2,
        "name": "Jane Smith",
        "email": "jane@example.com",
        "roles": ["user"],
        "active": false
    }
]

CSV Output

1
2
3
id,name,email,roles,active
1,John Doe,john@example.com,"[""admin"",""editor""]",true
2,Jane Smith,jane@example.com,"[""user""]",false

2 Where it ends up

Spreadsheets

Pick Semicolon when the file is for Excel in most of Europe. Excel splits on the list separator of the system locale, so a comma file opens there as a single column. Google Sheets lets you choose the separator on import.

Bulk database import

MySQL's LOAD DATA INFILE and PostgreSQL's COPY both read CSV. Neither reads a JSON array directly.

Reports

Turn a day of API responses or log lines into a flat file that someone else can sort and filter.

How to use the JSON to CSV tool

  1. The input is an array of JSON objects. Paste it into the left editor, or click Upload to load a .json file. Dummy fills in a sample so you can see the expected shape first.
  2. Pick a separator from the dropdown: comma, semicolon, tab or pipe. Custom lets you type your own.
  3. The CSV appears on the right as you type, with the object keys as the header line.
  4. Click Beautify to reformat the JSON input when you need to read it.
  5. Click Copy for the clipboard or Download to save a .csv file.

How to convert JSON to CSV in JavaScript, PHP and Python

JavaScript (Node.js)

const rows = [
  { id: 1, name: "Ada, Lovelace", city: "London" },
  { id: 2, name: 'Grace "Amazing" Hopper', city: "New York" },
];

const escape = (v) => {
  if (v === null || v === undefined) return "";
  const s = typeof v === "object" ? JSON.stringify(v) : String(v);
  return /[",\r\n]/.test(s) ? '"' + s.replace(/"/g, '""') + '"' : s;
};

const headers = Object.keys(rows[0]);
const csv = [
  headers.join(","),
  ...rows.map((r) => headers.map((h) => escape(r[h])).join(",")),
].join("\r\n");

console.log(csv);

For large or irregular data use the papaparse package: Papa.unparse(rows) handles quoting and mixed keys.

PHP

<?php
$json = '[{"id":1,"name":"Ada, Lovelace","city":"London"},
          {"id":2,"name":"Grace \"Amazing\" Hopper","city":"New York"}]';
$rows = json_decode($json, true, 512, JSON_THROW_ON_ERROR);

$out = fopen('php://output', 'w');
fputcsv($out, array_keys($rows[0]), ',', '"', '\\', "\r\n");
foreach ($rows as $row) {
    fputcsv($out, $row, ',', '"', '\\', "\r\n");
}
fclose($out);

PHP 8.4 warns if you omit the $escape argument. The sixth argument sets the line ending and needs PHP 8.1. Swap php://output for a file handle to write to disk.

Python

import csv, json, sys

rows = json.loads('[{"id":1,"name":"Ada, Lovelace","city":"London"},'
                  '{"id":2,"name":"Grace \\"Amazing\\" Hopper","city":"New York"}]')

writer = csv.DictWriter(sys.stdout, fieldnames=rows[0].keys(), lineterminator="\r\n")
writer.writeheader()
writer.writerows(rows)

# To a file instead: open("out.csv", "w", newline="", encoding="utf-8-sig")

Pass newline="" when opening the file, or on Windows every line ending comes out doubled.

Common errors and how to fix them

All the data lands in one Excel column
Excel uses the list separator from the operating system locale, which is a semicolon in most of Europe. Export with the semicolon separator, or use Data > From Text/CSV in Excel and set the delimiter there.
Accented characters show as é or ??? in Excel
Excel assumes a legacy code page for files without a byte order mark. Prepend the UTF-8 BOM (EF BB BF) to the file, or open it through the import wizard and choose UTF-8.
Leading zeros disappear (00123 becomes 123)
Spreadsheets treat the column as numeric. The CSV is correct; format the column as Text in the spreadsheet, or write the value as ="00123" if you control the export.
Nested objects appear as {"a":1} inside a cell
CSV is flat, so the tool writes nested values as JSON strings. Flatten the objects to dotted keys such as address.city before converting if you need separate columns.

Good to know

RFC 4180 is the closest thing CSV has to a standard, and it is short: CRLF line endings, double quotes around any field that contains the delimiter, a quote or a line break, and inner quotes doubled. Most parsers accept LF too. Emit CRLF when the file is headed for Excel, since Windows tools are the strictest about it.

CSV has no types. Every value is text until the reader guesses, and spreadsheets guess badly: Excel turns 1e5 into 100000 and 3-4 into a date. When values must survive the round trip intact, use the JSON to XLSX tool instead.

Specifications and references

This tool follows the specifications below. Read them when you need the exact normative wording, for example to settle an edge case between two implementations.

Guides and tutorials

Frequently Asked Questions

How are nested objects handled?

Nested objects and arrays are written as a JSON string inside a single CSV cell, so no data is lost and the table stays valid. Parse that cell again later, or flatten the objects to dotted keys such as address.city first if you want separate columns.

Can I use a custom delimiter?

Yes. Choose Custom in the separator dropdown and type any character or string, or pick a preset: comma, semicolon, tab or pipe. Use semicolon for Excel in most European locales.

What should the JSON look like?

An array of flat objects works best: each object becomes a row and each key a column. A single object becomes a one-row table. The header is the union of every object's keys, so a key missing from some objects leaves empty cells in those rows.

Are values with commas or quotes escaped correctly?

Yes. A field that contains the delimiter, a double quote or a line break is wrapped in double quotes, and inner quotes are doubled, following RFC 4180. Lines end with LF rather than the CRLF the RFC specifies, which Excel and Google Sheets both read without trouble.

Can I download the result as a .csv file?

Yes. Download saves the output as a .csv file, and Copy puts it on the clipboard.

Related

CSV to JSON converts in the other direction. When the recipient needs a real spreadsheet file with typed cells, use JSON to Excel. Run input that will not parse through the JSON formatter first.

More Data Converters

Browse all data converters

Copied to clipboard!