Skip to content

Instantly share code, notes, and snippets.

@knudmoeller
Last active July 12, 2023 07:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knudmoeller/82d1bb2fce4af07b202a1f194ba44961 to your computer and use it in GitHub Desktop.
Save knudmoeller/82d1bb2fce4af07b202a1f194ba44961 to your computer and use it in GitHub Desktop.
Turn a JSON array into CSV

input data.json

[
  {
    "id": 0 ,
    "foo": "bar"
  } ,
  {
    "id": 1 ,
    "foo": "baz"
  } ,
  {
    "id": 2 ,
    "foo": "dingo"
  }
]
cat data.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $cols, $rows[] | @csv'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment