简体   繁体   中英

JQ query output in csv format

I have been trying to extract a csv from the below json file using jq but not able to get so far. Does any experts out here can help?

 { "values": [ { "resourceId": "xxxx-xxxx-xxx-8b16-xxxxxx", "property-contents": { "property-content": [ { "statKey": "config|name", "timestamps": [ 1517591034069 ], "values": [ "somebname.UNIVERSE.test.com" ] }, { "statKey": "summary|guest|ipAddress", "timestamps": [ 1517591034069 ], "values": [ "100.xx.5.xx" ] }, { "statKey": "summary|parentCluster", "timestamps": [ 1551120506024 ], "values": [ "UFO-UFO" ] }, { "statKey": "summary|parentDatacenter", "timestamps": [ 1551120806021 ], "values": [ "GALAXY-D123" ] }, { "statKey": "summary|parentVcenter", "timestamps": [ 1517591334271 ], "values": [ "X-RAY123" ] }, { "statKey": "summary|runtime|powerState", "timestamps": [ 1517591034069 ], "values": [ "Powered On" ] } ] } }, .. ...

xxx-xxxx-xxx-8b16-xxxxxx,somebname.UNIVERSE.test.com,100.xx.5.xx,UFO-UFO,GALAXY-D123,X-RAY123,Powered On

Expected o/p is:

xxx-xxxx-xxx-8b16-xxxxxx,somebname.UNIVERSE.test.com,100.xx.5.xx,UFO-UFO,GALAXY-D123,X-RAY123,Powered On

Your expected output leaves some things unclear:

The second CSV column contains somebname.UNIVERSE.test.com , which was presumably derived from the section "property-content": [ {..., "values": [ "somebname.UNIVERSE.test.com" ], ... } . How do you determine which element in the "property-content" list to pick for the second column? Is it because it's the first element? Is it because of its "statKey": "config|name" ?

What if the "property-content" list is empty? What if it doesn't have the "statKey" entry you're looking for? What if the "values" list has zero or more than one element? The CSV row can only contain one scalar value. The same question applies for subsequent columns.

Making a wild guess here,

$ jq -r '.values[] | [ .resourceId, (."property-contents"."property-content"[] | .values[]) ] | join(",")' your.json
xxxx-xxxx-xxx-8b16-xxxxxx,somebname.UNIVERSE.test.com,100.xx.5.xx,UFO-UFO,GALAXY-D123,X-RAY123,Powered On

I cannot guarantee (and somewhat doubt) that this works in the general case, but I've been unable to extract a general case from your one example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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