Skip to content
Technology·1 min read·

JSON to CSV

Turn a JSON array of objects into CSV, with a header row taken from the object keys.

The answer

The input must be an array of objects. Keys become the header, in the order they first appear. Cells that contain a comma, a quote, or a line break are quoted.

Result

CSV

name,city
Ada,London

Data rows

1

What do you want to do next?

How this works

Each object is one row. A key that appears only on a later object is still given a column, and earlier rows leave that cell empty. Nested objects and arrays are written as JSON text inside the cell.

Quoting follows the usual CSV rule: a cell with a comma, quote, or line break is wrapped in double quotes, and a quote inside the cell is doubled.

Practical steps

  1. 1

    Paste an array of objects, not a single object.

  2. 2

    Check the header row against the keys you expected.

  3. 3

    Open the CSV in a spreadsheet and confirm that quoted commas stayed in one cell.

Examples

  • One person

    [{"name":"Ada","city":"London"}] becomes a header of name,city and one data row.

Common mistakes

  • ×

    Pasting a JSON object of arrays and expecting those arrays to become columns.

Frequently asked questions

Can I convert one object?

Wrap it in an array first. A bare object is rejected.

What happens to null?

A null or missing value becomes an empty cell.

Sources