Convert JSON to CSV with jq: Commands That Work
The command line tool jq can turn JSON into CSV with its @csv formatter. It is fast, scriptable and good for big files. The trick is that @csv wants an array of values for each row, so you build those arrays yourself.
An array of objects, all fields
jq -r '(.[0] | keys_unsorted) as $k | $k, (.[] | [.[$k[]]]) | @csv' data.json > out.csv
This takes the keys of the first object as the header, then writes each object as a row in the same order. The -r flag prints plain text instead of JSON strings. It only works if every record has the same flat keys, since the header comes from the first one.
Chosen fields, including nested ones
Most of the time you want specific columns. List them yourself, and reach into nested objects with dots such as .address.city:
jq -r '("id,name,city" | split(",")), (.[] | [.id, .name, .address.city]) | @csv' data.json
The first part prints the header, and the second prints one row per object. A missing key becomes null, which @csv writes as an empty cell.
Arrays inside records
A list cannot go into @csv as it is. Join it into one cell, or use .items[] to print one row per item:
jq -r '.[] | [.id, (.tags | join(";"))] | @csv' data.json
JSON Lines
For one object per line, drop the outer .[], since jq already reads each line as a separate input:
jq -r '[.id, .name, .address.city] | @csv' data.jsonl
Print the header with a separate echo before it. See JSONL vs JSON for the format.
What @csv does for you
- Text is wrapped in double quotes, and quotes inside it are doubled.
- Numbers and booleans are written without quotes, and null becomes an empty cell.
- Commas and line breaks inside text are safe because the text is quoted.
- Use
@tsvfor tab separated output.
Watch out for
- Large integers. Older jq versions convert numbers to 64-bit floats, which can round IDs above 9007199254740991. Newer versions keep the original digits when a number is passed through unchanged, but check your version, or make IDs strings.
- Windows line endings. jq writes plain line feeds. Excel handles them, but some tools expect CRLF.
- Excel and encoding. jq writes UTF-8 with no byte order mark, so Excel may garble accents. See CSV garbled or in one column.
Without the terminal
If you would rather not write a filter, paste the JSON into the JSON to CSV converter. It picks the columns, flattens nested objects, and runs in your browser.