Sample Header Ad - 728x90

Convert json to csv with headers in jq

4 votes
2 answers
8518 views
Is it possible to convert this json:
[
    {
        "bytes": 276697,
        "checked": false
    },
    {
        "bytes": 276697,
        "checked": false
    }
]
to a table WITH headers in jq? I've tried:
cat file.json | jq '.[] | join(",")'
but it omits headers:
"276697,false"
"276697,false"
it should be:
"bytes,checked"
"276697,false"
"276697,false"
I hoped that you can just run two commands:
cat file.json | jq '.[] | keys, .[] | join(",")'
but the second one fails:
"bytes,checked"
jq: error (at :64): Cannot iterate over null (null)
Ideally it would be simpler than this .
Asked by Daniel Krajnik (371 rep)
Aug 25, 2023, 05:23 PM
Last activity: Feb 3, 2025, 11:25 PM