Skip to content

Instantly share code, notes, and snippets.

@ryenski
Last active April 11, 2024 20:14
Show Gist options
  • Save ryenski/19812a565d2855a545cd5972792debde to your computer and use it in GitHub Desktop.
Save ryenski/19812a565d2855a545cd5972792debde to your computer and use it in GitHub Desktop.
Convert json to csv with jq @csv filter

To convert json to csv with headers using jq and the @csv filter.

jq -r '[first|keys_unsorted] + map([.[]]) | .[] | @csv' example.json > converted.csv

This would work on a simple json structured like:

[
  {
    "dealName": "Example 1",
    "filename": "example1.txt",
    "dealId": 12345,
    "documentId": 67890
  },
  {
    "dealName": "Example 2",
    "filename": "example2.txt",
    "dealId": 23456,
    "documentId": 45678
  }
]

Output:

"dealName","filename","dealId","documentId"
"Example 1","example1.txt",12345,67890
"Example 2","example2.txt",23456,45678
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment