我有以下jq
命令:
cat myFile.json | jq -r '.tickets[] | [.created_at, .id, .via.channel, .tags[]] | @csv'
它输出一行如:
"2016-02-02T10:00:00Z",99999,"web","tag1","tag2","tag3","tag4"
我正在尝试join
.tags[]
数组,以便我可以获得:
"2016-02-19T13:25:55Z",99999,"web","tag1,tag2,tag3,tag4"
我尝试过一些事情,比如
cat myFile.json | jq -r '.tickets[] | [.created_at, .id, .via.channel, (.tags[] | join(","))] | @csv'
但它会产生错误,例如
jq: error (at <stdin>:0): Cannot iterate over string ("tag1...)
那么,如何在上面的命令中加入.tags[]
,以便我得到一个单独的字符串值(包含逗号分隔的标记值)而不是单独的字段?
答案 0 :(得分:11)
您需要在join()
列表中调用tags
,而不是单个标记。试试:
jq -r '.tickets[] | [.created_at, .id, .via.channel, (.tags | join(","))] | @csv'