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.
JSON repair guides
Topic hubs
- JSON Parse Errors: Read the Message, Jump to the Fix
- Fix Invalid JSON: From 'What's Wrong' to a Clean File
- JSON Formatter, Validator, Viewer: Pick the Right Tool
- Repair LLM JSON Output: Handling Almost-JSON from AI
- Privacy: JSON Tools That Don't Leave Your Browser
- JSON Interop: YAML, CSV, XML, JWT, Schema
Specific guides
- How to Decode Base64 Strings (and JWT Payloads)
- URL Encoding: Percent-Encode Query Parameters and Paths
- Convert YAML to JSON (and Avoid Indentation Errors)
- Convert JSON to XML: Root Elements, Attributes, and Arrays
- Escape JSON as a String Literal (and Decode Double-Encoded JSON)
- Fix Trailing Comma in JSON
- Fix Single Quotes in JSON
- Fix Unquoted Keys in JSON
- Repair LLM JSON Output
- Fix JSON Parse Error: Expected Property Name
- JSON vs JS Object Literal: The Key Differences
- Validate JSON Before API Requests
- JSON Formatter vs JSON Repair
- Fix JSON Unexpected Token Errors
- JSON to JavaScript Object Converter