[英]Export json via jq to csv
我有這個 output 作為 test.json (它是 AWS 的摘錄,但我已經更改了名稱)
[
{
"InstanceId": "I-1234",
"Vol": "vol-5678",
"Delete": false,
"State": "in-use",
"Tags": [
{
"Key": "Size",
"Value": "large"
},
{
"Key": "Colour",
"Value": "red"
},
{
"Key": "Shape",
"Value": "square"
},
{
"Key": "Weight",
"Value": "light"
}
]
}
]
我想導出特定字段,包括所有標簽到 csv,所以它看起來像這樣:
id,vol,state,size,colour,shape,weight
value,value,value,value,value,value,value
我已經運行了這個:
貓測試。json | jq -c ' { id: .[].InstanceId, vol: .[].Vol, 標簽: .[].Tags | map ( [.Key, .Value] | 加入 (":")) | @csv } ' >> 測試.csv
它看起來像這樣:
cat test.csv
{"id":"I-1234","vol":"vol-5678","tags":"\"Size:large\",\"Colour:red\",\"Shape:square\",\"Weight:25kg\""}
如果我在 Excel 打開,看起來像:
{"id":"I-1234" vol:"vol-5678" tags:"\"Size:large\" \"Colour:red\" \"Shape:square\" \"Weight:25kg\""}
我將在許多 aws 資源上進行循環,並希望繼續附加到 csv。
I want to remove
{ } at beginning and end.
the key description I would like at top as a header, rather than to the left of the value..
so for: "id":"I-1234" vol:"vol-5678"
I would like
id, vol
I-1234, vol-5678
and the same with the Tags
remove the Array Name: "tags:" ( think its the array name, I'm not a developer, infrastructure dude! ) and just leave
Size,Colour,Shape,Weight, ...
large,red,square,25kg, ...
Can anyone help, point me in the right direction ..
thanks .. :)
jq -r '
["Size","Colour","Shape","Weight"] as $Keys
| (["id", "vol"] + ($Keys|map(ascii_downcase))),
( .[]
| (.Tags|from_entries) as $dict
| [.InstanceId, .Vol, $dict[$Keys[]]] )
| @csv
'
這將產生有效的 CSV,列按所需順序排列,而與 .Tags 數組中的項目順序無關。
如果您不希望引用行中的字符串,那么(冒着沒有有效 CSV 的風險)可以考慮的一種選擇是將上面的@csv
替換為join(",")
。 或者,您可能希望考慮使用@tsv
,然后用逗號替換制表符(例如,使用sed
或tr
甚至jq
:-)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.