Formats

Convert JSON to CSV/TSV with jq (@csv, @tsv)

3 min read

@csv and @tsv format an array of values as one delimited line, with correct quoting/escaping. Use -r so the output isn’t wrapped in JSON quotes.

Example input

[
  { "name": "Ada",   "age": 36, "city": "London" },
  { "name": "Linus", "age": 54, "city": "Portland" }
]

Rows only

jq -r '.[] | [.name, .age, .city] | @csv' data.json

Output:

"Ada",36,"London"
"Linus",54,"Portland"

With a header row

Emit the header, then the rows:

jq -r '["name","age","city"], (.[] | [.name, .age, .city]) | @csv' data.json
"name","age","city"
"Ada",36,"London"
"Linus",54,"Portland"

Header from the keys automatically

jq -r '(.[0] | keys_unsorted) as $k | $k, (.[] | [.[$k[]]]) | @csv' data.json

TSV (tab-separated) instead

jq -r '.[] | [.name, .age] | @tsv' data.json

Save to a file

jq -r '.[] | [.name, .age, .city] | @csv' data.json > out.csv

Always pair @csv/@tsv with -r — otherwise you get a JSON string with escaped quotes instead of a raw CSV line. @csv quotes strings and escapes embedded quotes/commas per RFC 4180; @tsv uses tabs and escapes tab/newline. Each must be applied to an array of scalars.

Open the full version (with copy buttons) ↗

← All recipes