Convert JSON to CSV in Python, JavaScript, and jq
How do I convert JSON to CSV in code?
In Python use pandas.json_normalize(data).to_csv("out.csv", index=False), which flattens nested objects into dot-notation columns automatically. In Node.js use the json2csv package. On the command line use jq -r '.[] | [.id, .name] | @csv' data.json with the -r flag.
Python with pandas
json_normalize flattens nested dicts into dot-notation columns in one call. Pass record_path to explode an array of objects into rows and meta to carry parent fields down onto each row.
Python one-liner
import json, csv, sys; data = json.load(open("data.json")); w = csv.DictWriter(sys.stdout, fieldnames=data[0].keys()); w.writeheader(); w.writerows(data). This works for flat arrays with no pandas install.
Node.js with json2csv
npm install json2csv, then const { parse } = require("json2csv"); const csv = parse(data, { flatten: true });. The package handles escaping, headers, and RFC 4180 quoting.
jq on the command line
The @csv filter takes an array and produces a comma-separated row. Always pair it with -r so the output is raw text instead of JSON-quoted, and name each nested path explicitly such as .user.name, because jq does not auto-flatten.
Handling nested objects in jq
Build each row as an array of paths, .[].user.name and .address.city, then pass the whole array to @csv. For a sub-array such as orders, iterate both levels and repeat the parent fields on each row.
Large files
pandas and json2csv process files in memory, which is fine up to hundreds of MB. For very large data, stream records or use a tool with streaming support.
FAQ
Does pandas need json_normalize for flat data?
No. json_normalize is for nested dicts. Flat arrays convert fine with pd.read_json and to_csv directly.
Why does jq need the -r flag?
Without it, jq outputs the CSV as a JSON string, with quotes and escapes. The -r flag prints the raw CSV text.
Which method handles nested arrays best?
pandas json_normalize with record_path and meta is the most convenient for arrays of objects. jq gives you full control for custom shapes.
Is there a no-install option?
Yes. An online converter runs in the browser and handles the same cases with no setup, which is ideal for one-off files.