Like that:
for id in $(jq -r '.all[].id' <<< "$json"); do
echo "$id"
done
Notice that -r option has to be used if you want to remove double quotes:
· --raw-output / -r:
With this option, if the filter´s result is a string then
it will be written directly to standard output rather than
being formatted as a JSON string with quotes. This can be
useful for making jq filters talk to non-JSON-based
systems.
The entire script could look like that:
#!/usr/bin/env bash
json='{
"all":[
{
"id":"51a"
},
{
"id":"52b"
},
{
"id":"53c"
}
]
}'
for id in $(jq -r '.all[].id' <<< "$json"); do
echo "$id"
done
But as noted in the comments, for x in $(...) is an antipattern and should not be used: https://mywiki.wooledge.org/DontReadLinesWithFor.
To assign two indices to 2 separate variables:
#!/usr/bin/env bash
json='{
"all":[
{
"id":"51a",
"name":"Steve"
},
{
"id":"52b",
"name":"Phoebe"
},
{
"id":"53c",
"name":"Dino"
}
]
}'
jq -r '.all[] | .id + " " + .name' <<< "$json" |
while read -r id name; do
echo id: "$id"
echo name: "$name"
done
while IFS= read -r id; do ...; done < <(jq ...)is a better way to run that loop. Right now, if you have an id of*, a list of filenames in your current directory will be inserted (among other issues; consider that one a representative example).