Skip to content

Instantly share code, notes, and snippets.

@mihai-craita
Last active January 13, 2021 11:19
Show Gist options
  • Save mihai-craita/9c6bf77e32a5c8a30363bf9ecc99bfaf to your computer and use it in GitHub Desktop.
Save mihai-craita/9c6bf77e32a5c8a30363bf9ecc99bfaf to your computer and use it in GitHub Desktop.
Convert json to csv

Using jq to generate a csv from a json file


The command will transform the json file users.json in csv, users.csv. The json has an array named items that contains different fields, here we select only id, name and the array perms. the tostring filter is applied to flatten the array perms

{
  "items": [
    {
      "id": "j_doe",
      "name": "Jon Doe",
      "perms": [
        30,
        104,
        102,
        ]
     },
   ]
}
jq -r '.items[] | [.id, .name, (.perms | length), .perms | tostring] | @csv' users.json >> users.csv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment