Making a CSV with jq

I’m finding jq to be a really useful tool for dealing with JSON formatted data.

I came across this Stack Overflow answer for creating a CSV from any array of JSON objects.

jq -r '
(
  map(keys)
  | add
  | unique
) as $cols
| map(
  . as $row
  | $cols
  | map($row[.])
) as $rows
| $cols, $rows[] | @csv'