jq: How to Convert JSON to CSV

Updated

Learn how to convert a JSON array to a CSV file with jq

jq: How to Convert JSON to CSV

Before we start, make sure you have jq installed on your computer. On a Mac, you can install it with Homebrew:

brew install jq

First, open your terminal and navigate to the folder with your JSON file. It should hold an array of objects. To pick the columns yourself, with a header row, run:

jq -r '["ID", "Name", "Email"], (.[] | [.id, .name, .email]) | @csv' input.json > output.csv

The first array is the header. .[] loops over each object and builds one row from the fields you list, and @csv adds the quotes and escaping. Just replace id, name and email with your own keys.

Don't skip -r. Without it, jq wraps every row in an extra pair of quotes. A missing value becomes an empty cell, and you can open output.csv in Numbers or Excel to check it.

If every object has exactly the same keys, you can take the header from the first one: (.[0] | keys_unsorted), (.[] | [.[]]). Be careful, though: if one object is missing a key, its values slide into the wrong columns.

undefined

For objects with different keys, collect every key first: (map(keys) | add | unique) as $cols, then look each one up per row. The full command is in the video description.