I'm looking at parsing nested json. For the example below, I know that it appears on Github - however due to sensitivity I cannot post my actual data on here.
I've Been looking at jq
for formatting, and can pull each component out, but cannot merge them together such that it looks like the below.
BEcuase of software restrictions, I cannot use 3rd party code.
input:
{ "asgs": [ { "name": "test1", "instances": [ {"id": "i-9fb75dc", "az": "us-east-1a", "state": "InService"}, {"id": "i-95393ba", "az": "us-east-1a", "state": "Terminating:Wait"}, {"id": "i-241fd0b", "az": "us-east-1b", "state": "InService"} ] }, { "name": "test2", "instances": [ {"id": "i-4bbab16", "az": "us-east-1a", "state": "InService"}, {"id": "i-417c312", "az": "us-east-1b", "state": "InService"} ] } ] }
output:
test1 i-9fb75dc us-east-1a InService
test1 i-95393ba us-east-1a Terminating:Wait
test1 i-241fd0b us-east-1b InService
test2 i-4bbab16 us-east-1a InService
test2 i-417c312 us-east-1b InService
EDIT: Current code is such that I loop through all the instances of instances
using and then append the names. For example:
cat sampleData.json | jq -c '.' | while read i; do
echo $i, & jq '.instances' sampleData.json
done
A slight shorter (though marginally more complex) version of @anubhava's answer:
jq -r '.asgs[] | .name as $n | (.instances[] | [$n, .id, .az, .state] | @tsv)' file.json
This "remembers" each name before producing a tab-separated line for each instance, inserting the correct name in each row.
You may use this jq
:
jq -r '.asgs[] | .name + "\t" + (.instances[] | .id + "\t" + .az + "\t" + .state)' file.json
test1 i-9fb75dc us-east-1a InService
test1 i-95393ba us-east-1a Terminating:Wait
test1 i-241fd0b us-east-1b InService
test2 i-4bbab16 us-east-1a InService
test2 i-417c312 us-east-1b InService
You can use a generic map
to create an extra entry per-instance:
jq -r '.asgs[] | [.name] + (.instances[] | map(.)) | @tsv'
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.