Convert JSON to CSV: Flatten an Array of Objects

A JSON array of objects maps to a CSV table — one row per object, columns from the union of keys. The real work is quoting and handling nested values.

The array-of-objects shape

CSV is a flat table, so the JSON that converts cleanly is an array of objects: each object becomes a row and each key becomes a column.

Building the header

Use the union of every object’s keys as the header, not just the first object’s, so records with extra or missing fields still line up. Missing cells are left blank.

Quoting rules

Any value containing a comma, double quote, or newline must be wrapped in double quotes, and embedded quotes doubled, or the columns will misalign.

Nested values

Objects and arrays have no flat CSV form, so write them as JSON text inside the cell (reversible) or flatten them into dotted columns like address.city.

Header order and stability

Sort the union of keys, or freeze the order to the first record's keys with extras appended. A stable header order makes diffs across exports useful and avoids reshuffling columns every run.

Date and number formatting

Spreadsheets reinterpret values aggressively: long numeric IDs lose precision, ISO dates get reformatted, and leading zeros disappear. If a value must round-trip exactly, wrap it in quotes and prefix with a single quote when pasting into Excel.

Excel and Google Sheets compatibility

Use CRLF line endings and a BOM (UTF-8 with byte-order mark) so Excel detects Unicode. For Google Sheets the BOM is optional, but commas as delimiters and double-quote escaping still apply.

Going from CSV back to JSON

Parse rows with quote-aware splitting (do not split on commas naively), use the first row as field names, and try to coerce numbers, booleans, and ISO dates per column. Round-tripping via this tool keeps strings as strings unless a column is uniformly numeric.

Common pitfalls

These are the issues that break most CSV exports.

  • Unescaped commas inside string values
  • Newlines in cells without surrounding quotes
  • Mixed encodings (UTF-8 vs Latin-1) producing mojibake
  • Large integers (IDs) silently truncated by spreadsheets
  • Boolean values stored as the strings "true"/"false" instead of TRUE/FALSE

See also

This guide handles one format boundary. The hub lists every JSON ↔ neighbor-format conversion with its standard and edge cases.