通过 jq 将 json 导出到 csv
Export json via jq to csv
我将此输出作为 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
我有运行这个:
猫test.json | jq -c ' { id: .[].InstanceId, vol: .[].Vol, tags: .[].Tags |映射 ( [ .Key, .Value] | join (":")) | @csv } ' >> test.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
:-)。
我将此输出作为 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
我有运行这个:
猫test.json | jq -c ' { id: .[].InstanceId, vol: .[].Vol, tags: .[].Tags |映射 ( [ .Key, .Value] | join (":")) | @csv } ' >> test.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
:-)。