简体   繁体   English

通过 jq 导出 json 到 csv

[英]Export json via jq to csv

I have this output as test.json ( Its an AWS extract, but I have changed the names )我有这个 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"
            }
        ]
    }
]

I want to export specific fields, including all tags to a csv, so it looks like this:我想导出特定字段,包括所有标签到 csv,所以它看起来像这样:

id,vol,state,size,colour,shape,weight
value,value,value,value,value,value,value

I have run this:我已经运行了这个:

cat test.json |猫测试。json | jq -c ' { id: .[].InstanceId, vol: .[].Vol, tags: .[].Tags | jq -c ' { id: .[].InstanceId, vol: .[].Vol, 标签: .[].Tags | map ( [.Key, .Value] | join (":")) | map ( [.Key, .Value] | 加入 (":")) | @csv } ' >> test.csv @csv } ' >> 测试.csv

And it looks like this:它看起来像这样:

cat test.csv
{"id":"I-1234","vol":"vol-5678","tags":"\"Size:large\",\"Colour:red\",\"Shape:square\",\"Weight:25kg\""}

if I open in Excel, looks like:如果我在 Excel 打开,看起来像:

{"id":"I-1234"  vol:"vol-5678"  tags:"\"Size:large\"    \"Colour:red\"  \"Shape:square\"    \"Weight:25kg\""}

I will be looping this over many aws resources, and would like to keep appending to csv.我将在许多 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
'

This will produce valid CSV, with the columns in the desired order, irrespective of the ordering of the items in the.Tags array.这将产生有效的 CSV,列按所需顺序排列,而与 .Tags 数组中的项目顺序无关。

If you don't want the strings in the rows to be quoted, then (at the risk of not having valid CSV) one option to consider would be replacing @csv above by join(",") .如果您不希望引用行中的字符串,那么(冒着没有有效 CSV 的风险)可以考虑的一种选择是将上面的@csv替换为join(",") Alternatively, you might wish to consider using @tsv and then replacing the tabs by commas (eg using sed or tr or even jq :-).或者,您可能希望考虑使用@tsv ,然后用逗号替换制表符(例如,使用sedtr甚至jq :-)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM