echo "$REPOS" | jq '.[].repositoryName' | while read -r repo; do echo "do something with $repo"; done

Output:

do something with "repo_a"
do something with "repo_b"
do something with "repo_c"
do something with "repo_d"

Or without quotes:

echo "$REPOS" | jq -r '.[].repositoryName' | while read -r repo; do echo "do something with $repo"; done

Output:

do something with repo_a
do something with repo_b
do something with repo_c
do something with repo_d
Answer from Cyrus on Stack Overflow
🌐
GitHub
github.com › jqlang › jq › issues › 2247
What's the simplest way to _iterate_ over a JSON array using `for` or `while`? · Issue #2247 · jqlang/jq
January 12, 2021 - How can I perform an operation over each object in the array? ... for i in "$(jq -r '.users[]' users.json)"; do name=$(echo $i | jq -r .description) height=$(echo $i | jq -r .height) echo "$name has id $height" done
Author   arielelkin
Discussions

JSON with jq into bash array / loop through - Unix & Linux Stack Exchange
I have a JSON result which I want to go through and send an alert, if one or several of the values in the JSON string is smashing thresholds. This bash command: for sat in `docker exec -i storagenode More on unix.stackexchange.com
🌐 unix.stackexchange.com
January 1, 2022
iteration - Output specific key value in object for each element in array with jq for JSON - Stack Overflow
$ for i in $(jq -r ".[] | .AssetId" input.json) do echo $i done 14462955 114385498 29715011 98253651 ... Find the answer to your question by asking. Ask question ... See similar questions with these tags. ... I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s... 82 How to extract a field from each object in an array ... More on stackoverflow.com
🌐 stackoverflow.com
Using jq to loop through file of JSON objects
Can you provide a sample of the json file and what output you're hoping to get? More on reddit.com
🌐 r/AskProgramming
4
2
July 9, 2020
How to loop through an array of objects?
I enjoy cooking. More on reddit.com
🌐 r/bash
3
5
May 2, 2022
🌐
Exercism
exercism.org › tracks › jq › concepts › objects
Objects in jq on Exercism
{first: "Jane", last: "Lopez", status: "awesome!"} | map_values(ascii_upcase) # => {"first": "JANE", "last": "LOPEZ", "status": "AWESOME!"} To iterate over an object, we must first convert it to an array of key-value objects.
🌐
Ilya's blog
ilya-sher.org › 2016 › 05 › 11 › most-jq-you-will-ever-need
Most JQ you will ever need – Ilya's blog
December 6, 2017 - aws cloudformation describe-stacks ... JSON formatting is in my .bashrc_aws. Given array of objects, iterate over it accessing fields in each array element....
Top answer
1 of 2
4

It's generally a good idea to avoid looping over the result of a command substitution. It's inelegant as the command in the substitution must finish executing before the loop can even start running, it's inefficient as the full result of the command in the substitution must be stored in memory, and it's prone to errors since the shell must be allowed to split the output of the command on whitespace and subject the resulting words to filename globbing.

It's better to use read in a while loop:

#!/bin/sh

curl -s 'localhost:14002/api/sno' |
jq -r '.satellites[].id' |
while IFS= read -r id; do
        curl -s 'localhost:14002/api/sno/satellite/'"$id"
done |
jq -r \
        --argjson auditScore 1 \
        --argjson suspensionScore 1 \
        --argjson onlineScore 0.9 \
        '.audits as a.satelliteName as $name |
        reduce ($ARGS.named|keys[]) as a[ARGS.named[$key] then (
                        . + ["\($key) below threshold: \(key]) for \($name)"]
                ) else . end
        ) | .[]'

This script assumes that you can contact your REST endpoints on localhost:14002 (the Docker container might be made to expose that port, for example). If you need to use the docker exec command to access the API, then replace the plain calls to curl with, e.g.

docker exec -i curl -s 'localhost:14002/api/sno'

For the updated question, using the api/sno/satellites endpoint:

#!/bin/sh

curl -s 'localhost:14002/api/sno/satellites' |
jq -r \
        --argjson auditScore 1 \
        --argjson suspensionScore 1 \
        --argjson onlineScore 0.9 \
        '.audits[] as a.satelliteName as $name |
        reduce ($ARGS.named|keys[]) as a[ARGS.named[$key] then (
                        . + ["\($key) below threshold: \(key]) for \($name)"]
                ) else . end
        ) | .[]'

Apart from a minor adjustment to the jq expression, this is essentially the same code as above, but bypassing the first curl call to fetch all the IDs, and the loop.

2 of 2
0

I‘ve found one possible answer on stackoverflow:


for sat in `docker exec -i storagenode wget -qO - localhost:14002/api/sno | jq .satellites[].id -r`
do
  docker exec -i storagenode wget -qO - localhost:14002/api/sno/satellite/$sat \
  | jq --raw-output \
    --argjson auditThreshold 1 \
    --argjson suspensionThreshold 1 \
    --argjson onlineThreshold 1 \
    '.audits
      | .satelliteName as $name
      | (
          [{auditScore}, $auditThreshold],
          [{suspensionScore}, $suspensionThreshold],
          [{onlineScore}, $onlineThreshold]
        )
      | select(.[0][] < .[1])
      | "\(.[0] | keys[]) (\(.[0][])) below threshold (\(.[1])) for \($name)"
    '
done
🌐
Reddit
reddit.com › r/askprogramming › using jq to loop through file of json objects
r/AskProgramming on Reddit: Using jq to loop through file of JSON objects
July 9, 2020 -

I know how to loop through an array of JSON objects and access values, but the data I have is not formatted as an array. It is simply a file full of JSON objects, separated by line. Is it still possible to parse this data with jq without formatting the file as a giant array?

Right now I'm essentially using a command like this:

cat file.json | jq '.[] | .name'
🌐
how.wtf
how.wtf › how-to-iterate-through-json-arrays-in-bash-using-jq.html
How to iterate through JSON arrays in Bash using jq | how.wtf
June 14, 2023 - 1readarray -t projects < <(jq -c ... array in an associative array called projects. Then, it uses a for loop to iterate through the objects and echo to print each object....
Find elsewhere
🌐
GitHub
gist.github.com › IAmStoxe › a36b6f043819fad1821e7cfd7e903a5b
This example shows you how to utilize jq to loop bash script through an array of JSON values. · GitHub
June 29, 2020 - for row in $(jq -r -c '.[]' ./json.json); do a=$(echo $row | jq -r '.a') b=$(echo $row | jq -r '.b') b=$(echo $row | jq -r '.c') echo $a echo $b echo $c done
🌐
Medium
wiemlimem.medium.com › iterating-through-json-file-in-bash-script-963d0b924b8b
The Simplest Way to Iterate Through Json Array in Shell Script | Medium
January 27, 2023 - #!bin/bash jsonData=$( cat jsonfile.json ) for row in $(echo $jsonData | jq -r '.[] | @base64'); do _jq() { echo "${row}" | base64 -di | jq -r "${1}" } # Set each property of the row to a variable id=$(_jq '.id') name=$(_jq '.name') username=$(_jq '.username') password=$(_jq '.password') # Utilize your variables echo "id=$id ; name=$name ; username=$username ; password=$password" done
🌐
Baeldung
baeldung.com › home › web › guide to linux jq command for json processing
Guide to Linux jq Command for JSON Processing | Baeldung on Linux
August 31, 2025 - First, we iterate over the array using .[]. Then we can pass each object in the array to the next filter in the command using a pipe |. The last step is to output the name field from each object using .name: ... We can also use a slightly more ...
🌐
Reddit
reddit.com › r/bash › how to loop through an array of objects?
r/bash on Reddit: How to loop through an array of objects?
May 2, 2022 -
[
  {
    "key": "key value blah blah 1",
    "secret": "secret value blah blah 1"
  },  {
    "key": "key value blah blah 2",
    "secret": "secret value blah blah 2"
  },  {
    "key": "key value blah blah 3",
    "secret": "secret value blah blah 3"
  },  {
    "key": "key value blah blah 4",
    "secret": "secret value blah blah 4"
  },

]

I need to match a given key with the keys in the array and assign the secret to a variable
something like:

for key in array; do 
    if key = "my key value to be matched"; then
        $secretvalue = $secretfromtheobject
    fi
done
echo  $secretvalue

This is just how I want it to be I know the code above is incomplete and wrong.

🌐
Tech Vomit
techvomit.net › posts › jq cheatsheet
JQ Cheatsheet | Tech Vomit
sample='[{"name":"foo"},{"name":"bar"}]' for row in $(echo "${sample}" | jq -r '.[] | @base64'); do echo ${row} | base64 --decode | jq -r '.name' done · Resource: https://www.starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/
🌐
jq
jqlang.org › manual
jq 1.8 Manual
Some filters produce multiple results, ... runs the second filter for each element of the array. Generally, things that would be done with loops and iteration in other languages are just done by gluing filters together in jq....
🌐
GitHub
gist.github.com › OlegGorj › 43ce94d8447d31da335db822b8682de9
looping through JSON array in shell script · GitHub
for k in $(jq '.children.values | keys | .[]' file); do value=$(jq -r ".children.values[$k]" file); name=$(jq -r '.path.name' <<< "$value"); type=$(jq -r '.type' <<< "$value"); size=$(jq -r '.size' <<< "$value"); printf '%s\t%s\t%s\n' "$name" "$type" "$size"; done | column -t -s$'\t'
🌐
HatchJS
hatchjs.com › home › how to iterate over an array in jquery
How to Iterate Over an Array in jQuery
January 5, 2024 - This means that you can iterate over the elements of an array, and then iterate over the elements of each of those elements, and so on. For example, if you have an array of arrays called `contacts`, you can iterate over it like this: $ jq '.contacts[].name' contacts.json "John Doe" "Jane Doe" "Mary Smith"
🌐
Stark & Wayne
starkandwayne.com › home
Egen is a technology services company working across cloud, data, ai, and platforms
July 24, 2024 - Egen is a technology services company with leading capabilities in cloud, data, AI, and platforms.
🌐
jQuery
api.jquery.com › jQuery.each
jQuery.each() | jQuery API Documentation
The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.