Shell Script: Iterating over array of json

bash, shell

Solution

Use `jq` to obtain the ids:

curl http://... | jq -r '.[].id'

You can pipe that into a bash `while` loop if you want to perform further processing:

curl http://... | jq -r '.[].id' | while read id ; do
    do_something "${id}"
done

Problem

I have an array of jsons which I need to iterate over. I do a curl call and get this result and need to process it for something. The array looks like this: ``` [ {"id": "f0345a01", "name": "scala1"}, {"id": "6f907cf", "name": "scala2"}, {"id": "d887f61", "name": "scala3"}, {"id": "5d07fca", "name": "scala5"}, {"id": "94ddaa", "name": "scala12"} ] ``` I need to get the id's from this array. I could not find any way to do so. I tried this following another stackoverflow question: ``` for i in "${arr[@]}" do echo "$i" done ```

Original source