arrays - Loop through json using jq to get multiple value -
here volumes.json :
{ "volumes": [ { "availabilityzone": "us-east-1a", "tags": [ { "value": "vol-rescue-system", "key": "name" } ], "volumeid": "vol-00112233", }, { "availabilityzone": "us-east-1a", "tags": [ { "value": "vol-rescue-swap", "key": "name" } ], "volumeid": "vol-00112234", }, { "availabilityzone": "us-east-1a", "tags": [ { "value": "vol-rescue-storage", "key": "name" } ], "volumeid": "vol-00112235", } ] }
i need both value of volumeid
, tags.value
used input invoke command. easy single value json array, not able extract multiple value , pass bash command.
i can single value using this:
cat volumes.json |jq -r '.volumes[].volumeid' |while read v; another_bash_command $v; done
but not able multiple value cause wrong:
cat volumes.json |jq -r '.volumes[].volumeid, .volumes[].tags[].value' |while read v w; another_bash_command $v $w; done
as loop 6 times of outcome instead of 3.
and, how pass multiple json value in loop bash array can use value in better way ? volumeid-> $arr[0][0]
, tags.value-> $arr[0][1]
, availabilityzone-> $arr[0][2]
...etc. have searched through , jq docs, , tried readarray
, still not able find out solution :( given.
it seems me want output 2 values (volumeid
, tags[].value
) on same line?
if that's case, simple string concatenation should enough:
$ jq -r '.volumes[] | .volumeid + " " + .tags[].value' volumes.json vol-00112233 vol-rescue-system vol-00112234 vol-rescue-swap vol-00112235 vol-rescue-storage
the above can used in pipeline while-read
:
$ cat my_script jq -r '.volumes[] | .volumeid + " " + .tags[].value' volumes.json \ | while ifs= read -r volumeid tagvalue; other_command "$volumeid" "$tagvalue" done
you should note if there more 1 element in tags
result reflect that. can avoided referring first element in tags
: .tags[0].value
Comments
Post a Comment