How to add a header to CSV export in jq?

后端 未结 3 2204
滥情空心
滥情空心 2021-02-18 23:30

I\'m taking a modified command from the jq tutorial:

curl \'https://api.github.com/repos/stedolan/jq/commits?per_page=5\' \\
| jq -r -c \'.[] | {mes         


        
3条回答
  •  既然无缘
    2021-02-19 00:04

    Based on Anton's comments on Jeff Mercado's answer, this snippet will get the key names of the properties of the first element and output them as an array before the rows, thus using them as headers. If different rows have different properties, then it won't work well; then again, neither would the resulting CSV.

    map({message: .commit.message, name: .commit.committer.name}) | (.[0] | to_entries | map(.key)), (.[] | [.[]]) | @csv

提交回复
热议问题