← All articles · April 21, 2026 · fmt.hjlabs.in

Convert JSON to CSV: 5 Methods Compared

JSON-to-CSV conversion sounds trivial until you encounter your first nested object, array of objects, or sparse JSON where keys differ between rows. Here are five battle-tested methods, ranked by use case.

The challenges

  1. Nested objects{ user: { name: "..." } } — flatten or preserve?
  2. Nested arrays{ tags: ["a", "b"] } — join, JSON-stringify, or explode?
  3. Inconsistent keys — different rows have different fields
  4. CSV escaping — commas in values, quotes in values, newlines in values
  5. Encoding — Excel doesn't open UTF-8 CSV correctly without BOM

Method 1: Browser-based tool (fastest for one-offs)

Paste JSON, get CSV. fmt.hjlabs.in/json-to-csv handles nested objects (auto-flattens with dot notation), preserves array order, and exports as Excel-compatible CSV.

Best for: data exports, ad-hoc conversions, sharing data with non-technical colleagues.

Method 2: jq (command line, scriptable)

# Simple flat JSON array
cat data.json | jq -r '(.[0] | keys_unsorted) as $keys | $keys, map([.[$keys[]]])[] | @csv' > data.csv

# With header
cat data.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv' > data.csv

Pros: no dependencies, scriptable in CI/CD, handles streams. Cons: incantation-heavy syntax, painful for nested data.

Method 3: Python (for serious data work)

import json, csv

with open('data.json') as f:
    data = json.load(f)

# Flat JSON array of objects
with open('out.csv', 'w') as f:
    writer = csv.DictWriter(f, fieldnames=data[0].keys())
    writer.writeheader()
    writer.writerows(data)

For nested JSON, use pandas.json_normalize():

import pandas as pd
df = pd.json_normalize(data)
df.to_csv('out.csv', index=False)

Method 4: Node.js (for backend pipelines)

const { Parser } = require('json2csv');
const data = require('./data.json');

const parser = new Parser();
const csv = parser.parse(data);
fs.writeFileSync('out.csv', csv);

The json2csv library handles nested fields, custom delimiters, BOM for Excel, and streaming for large files.

Method 5: Excel / Google Sheets (for non-coders)

Google Sheets has built-in JSON import via the IMPORTDATA function, or paste JSON into a cell and use Apps Script.

Excel: Power Query → Get Data → From JSON. Handles nested data with a UI.

Edge cases and how each handles them

Commas/quotes/newlines in values

All five methods handle this correctly when the value is properly quoted. RFC 4180 says: wrap in ", escape internal " as "".

name,description
"Smith, John","He said ""hello"""

Inconsistent keys (sparse JSON)

Best handled by Python pandas (auto-fills NaN) and our browser tool (auto-fills empty). jq and json2csv require explicit field list.

Nested objects

Standard flattening: { user: { name: "X" } } → column user.name. Most tools do this; jq doesn't natively (you write the path).

Arrays of values

Three options:

Pick based on what the consumer expects. Excel users prefer pipe-join.

Excel UTF-8 issue

Excel on Windows doesn't auto-detect UTF-8. Workaround: prepend a BOM (\xEF\xBB\xBF) to the file. Most modern tools do this when you set encoding: 'utf-8-sig'.

When to skip CSV

For data with deep nesting or arrays-of-objects, CSV is the wrong format. Consider:

Decision matrix

NeedBest tool
One-off, share with colleaguefmt.hjlabs.in/json-to-csv
Bash pipeline / CI/CDjq
Data science, big filespandas
Backend servicejson2csv (Node)
Non-coder workflowExcel Power Query

Bottom line

For 80% of conversions, a browser tool is fastest. For automation, learn jq. For complex nested data, use pandas. Don't try to roll your own CSV escaping — RFC 4180 has more edge cases than you'd expect.

Try the free dev tools suite

15+ tools. 100% client-side. No signup. No tracking.

Browse all tools →

More from the blog