json-to-csv.ts × README.md package.json Ln 1, Col 1

JSON to CSV Converter

Convert JSON arrays to CSV format for spreadsheet use.

@ local-runtime @ typescript @ 0 deps
$ ./json-to-csv ▶ running
// docs / json-to-csv.md

Getting API Data Into a Spreadsheet Without Writing a Script

The typical path from a JSON API response to a spreadsheet involves writing a Python or JavaScript script, importing a library, handling nested objects, writing a CSV output function, and running the script. For a one-time data export or a quick analysis, that is significant overhead. A JSON-to-CSV converter eliminates the script and gets the data into Excel or Google Sheets in under a minute.

Why This Conversion Is Nontrivial to Do Manually

Pasting JSON into a spreadsheet produces one cell with the entire JSON string. You cannot split it with a simple find-replace because JSON uses commas inside string values, nested brackets, and escaped characters — all of which break naive delimiter-splitting. A proper converter parses the JSON structure, extracts column headers from object keys, and serialises each row correctly including handling commas, quotes, and newlines inside values.

How the Conversion Works

The converter expects a JSON array of objects — the most common format returned by REST APIs when requesting collections:

[{"id": 1, "name": "Alice", "role": "admin"}, {"id": 2, "name": "Bob", "role": "user"}]

The process:

  1. Parse the JSON to validate it and build an in-memory structure.
  2. Extract column headers by scanning all objects for their keys. If different objects have different keys (a common inconsistency in real API data), the column set is the union of all keys encountered — rows missing a key get an empty cell for that column.
  3. Serialise each row, wrapping field values in double quotes if they contain commas, double quotes, or newlines. Double quotes within values are escaped by doubling them (He said "hello""He said ""hello""").
  4. Output the CSV with a header row followed by data rows.

Handling Nested Objects and Arrays

Real API responses often have nested structure: a user object with an embedded address object, or a product with an array of tags. Flat CSV cannot represent nested structure natively, so nesting is handled by flattening:

  • Nested objects are flattened using dot notation: {"address": {"city": "NYC"}} → column address.city with value NYC.
  • Arrays as values are serialised as a JSON string in the cell: {"tags": ["a", "b"]} → cell contains ["a","b"]. For deeply nested or array-heavy data, a scripted approach with explicit flattening logic will produce cleaner output.

Regional CSV Format Differences

Excel in regions that use comma as a decimal separator (most of Europe, Latin America) uses semicolon as the CSV field delimiter to avoid ambiguity. A CSV exported with commas will not open correctly in these Excel installations. Switch the delimiter to semicolon or use tab-separated values (TSV) if your audience uses non-English locale Excel settings.

// faq

What if my JSON objects have different keys? +

The converter takes the union of all keys found across all objects and uses that as the column set. Objects that are missing a key get an empty cell for that column. This handles inconsistent API responses gracefully. If an object has an extra key that others do not, the extra key becomes a column — rows without it will simply have an empty value.

What if my JSON is an object, not an array? +

The converter expects an array of objects as the top-level structure. If your JSON is a single object (like {\"users\": [...], \"total\": 100}), you need to extract the relevant array first. Paste the full JSON, identify the key that contains the array (e.g., \"users\"), and use that array as the input. Some API responses wrap arrays in a data or results key.

How are commas and quotes inside values handled? +

Values containing commas, double quotes, or newlines are wrapped in double quotes in the CSV output. Double quotes within a value are escaped by doubling them — this is the standard CSV quoting convention (RFC 4180). For example, the value She said "hello" becomes "She said ""hello""" in the CSV. Excel and Google Sheets handle this correctly.

Can I convert CSV back to JSON? +

Not with this tool — it is a one-direction converter. To convert CSV to JSON, you need a CSV parser that reads the delimiter and quoting rules correctly, then maps each row to a JSON object using the header row as keys. Many online tools and libraries (PapaParse in JavaScript, csv module in Python) handle this conversion.

main ⚡ 6 tools Ln 1, Col 1 UTF-8 LF TypeScript