Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active December 3, 2020 14:14
Show Gist options
  • Save gemmadlou/85337274246394e12f4e0e24ef913cca to your computer and use it in GitHub Desktop.
Save gemmadlou/85337274246394e12f4e0e24ef913cca to your computer and use it in GitHub Desktop.
JQ. Cheatsheet

JQ

Key/Value Shape

[{"Name": "Jonny Bravo","Age": "58"},{"Name": "Dexter","Age": "34"}]

Map JSON to CSV

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

Output

"Name","Age"
"Jonny Bravo",58
"Dexter",34

## Array Shape

[["Name", "Age"],["Jonny Bravo", 58],["Dexter", 34]]

Map JSON to CSV

cat example.json | jq -r '.[] | @csv'

Output

"Name","Age"
"Jonny Bravo",58
"Dexter",34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment