JSON to CSV Tutorial: Convert Arrays to Spreadsheet Rows

Last updated:

JSON to CSV conversion maps each object in a flat array to 1 CSV row and each key to a column header — a 100-object array with 5 keys produces a 100-row, 5-column CSV in under 10 ms. Nested JSON requires a flattening step first: {"user": {"name": "Alice"}} must become {"user.name": "Alice"} before the column can be written. JavaScript needs about 5 lines with Object.keys and Array.map; Python's csv.DictWriter handles it natively; the json2csv npm package handles 8 edge cases automatically. This guide covers flat array conversion, nested object flattening, arrays-of-arrays, header extraction across mismatched objects, quoting commas and newlines in cell values, and null vs empty string handling.

Start with a JSON array of objects

CSV is a table format, so the cleanest input is a list of similar records.

[
  {
    "id": 1,
    "name": "Alice",
    "email": "alice@example.com",
    "active": true
  },
  {
    "id": 2,
    "name": "Bob",
    "email": "bob@example.com",
    "active": false
  }
]
id,name,email,active
1,Alice,alice@example.com,true
2,Bob,bob@example.com,false

Choose CSV headers

Headers usually come from the union of keys across all objects. If the first object does not include every key, scan the whole array before writing the header row.

[
  { "id": 1, "name": "Alice" },
  { "id": 2, "name": "Bob", "team": "Support" }
]

// Headers should be: id,name,team

Flatten nested JSON objects

CSV cannot store nested objects directly. A common convention is dot notation.

[
  {
    "id": 1,
    "name": "Alice",
    "address": {
      "city": "Paris",
      "country": "FR"
    }
  }
]

// id,name,address.city,address.country
// 1,Alice,Paris,FR

Escape commas, quotes, and newlines

CSV values that contain commas, double quotes, or line breaks must be wrapped in double quotes. Double quotes inside the value are escaped by doubling them.

[
  { "note": "Hello, world" },
  { "note": "She said \"yes\"" }
]

// note
// "Hello, world"
// "She said ""yes"""

Common JSON to CSV mistakes

  • Trying to convert one deeply nested object without deciding what the rows are.
  • Using only the first object to determine headers when later rows have extra keys.
  • Flattening arrays into ambiguous text without a delimiter convention.
  • Forgetting that spreadsheet apps may reinterpret IDs, dates, and long numbers.

Convert JSON to CSV online

Paste a JSON array into Jsonic's JSON to CSV converter to flatten nested objects, copy the CSV, or download a spreadsheet-ready file.

Open JSON to CSV

Frequently asked questions

What happens to nested objects when converting JSON to CSV?

Nested objects must be flattened. The standard convention is dot notation: a nested field like address.city is written as a column header in the CSV output. Deeply nested structures or arrays of objects inside objects are poor fits for CSV and may need a different output format.

How do I handle arrays in JSON to CSV conversion?

Arrays of primitives can be joined into one cell with a delimiter. Arrays of objects can be flattened with indexed column names (items.0.name) or denormalized by repeating the parent row per array item. If arrays are central to the data model, CSV may not be the right format.

What if JSON objects have different keys?

Use the union of all keys across every object as the header row. Scan all objects before writing headers. Objects missing a key produce an empty cell. Using only the first object for headers is a common mistake that misaligns data in later rows.

How do I set the CSV delimiter?

The default is a comma. Use semicolons for European locales where commas are decimal separators. Use tabs for TSV files common in data pipelines. Most libraries accept a delimiter option — PapaParse, Python csv.writer, and most online tools all support custom delimiters.

Does JSON to CSV work with deeply nested data?

Poorly. CSV is two-dimensional. Three or more levels of nesting produce unreadably long column names. Arrays of objects nested inside arrays require indexed columns that leave many cells empty. Deeply nested JSON is usually better kept as JSON or stored in a relational database.

What tool converts JSON to CSV fastest?

Jsonic converts JSON to CSV in the browser instantly. For the command line, jq with @csv is very fast. For large files (100MB+), DuckDB or csvkit handle streaming conversion. The json2csv Node.js package supports streaming for large files without loading everything into memory.