0

I am using a curl command to get json data from an application

the example curl response

{
    "count": 2,
    "value": [
        {
            "id": 344,
            "name": "ip-172-20-94-68",
            "status": "offline",
       
        },
        {
            "id": 345,
            "name": "ip-172-20-95-119",
            "status": "offline",
        }
    ]
}

My bash script

 ipAddresses=$(curl -s -X GET "$hostUrl" -u :"$EncodedPAT" | jq -r ".value[] | select(.status==\"offline\").name")
 ids=$(curl -s -X GET "$hostUrl" -u :"$EncodedPAT" | jq -r ".value[] | select(.status==\"offline\").id")

        for ipAddress in "${ipAddresses[@]}"
            do
                for id in "${ids[@]}"
                    do
                    echo "ipAddress: ${ipAddress} id: ${id}"
                done
        done

output

ipAddress: ip-172-20-94-68
ip-172-20-95-119
ip-172-20-95-113
ip-172-20-94-96
ip-172-20-94-86
id: 344
345
346
348
350

So it looks like it is only iterating through the outermost loop once, and not iterating through the inner loops (just uses first element).

expected output

ipAddress: ip-172-20-94-68 id: 344
ipAddress: ip-172-20-95-119 id: 345
ipAddress: ip-172-20-95-113 id: 346
ipAddress: ip-172-20-94-96 id: 348
ipAddress: ip-172-20-94-86 id: 350

I've looked elsewhere on the net but am having trouble finding anything relevant.

Any guesses as to what might be wrong?

6
  • A nested loop does exactly what you're seeing. You don't want that. Just loop once, by index. Commented Nov 3, 2022 at 11:38
  • Or -- here's a thought -- why not have just one jq call give you pairs as its output? You'd also stop calling curl twice that way, and thus create less load on the server. Commented Nov 3, 2022 at 11:39
  • @CharlesDuffy thanks i have not thought of that im kinda new to jq Commented Nov 3, 2022 at 11:39
  • while IFS=$'\t' read -r name id; do echo "IP address: $name; id: $id"; done < <(curl -s -X GET "$hostUrl" -u :"$EncodedPAT" | jq -r '.value[] | select(.status=="offline") | [.name, .id] | @tsv') Commented Nov 3, 2022 at 11:41
  • (of course, if the only thing you're trying to do is generate the strings as output, that can be done just in jq alone with no shell loop at all: curl -s -X GET "$hostUrl" -u :"$EncodedPAT" | jq -r '.value[] | select(.status=="offline") | "ipAddress: \(.name) id: \(.id)"' Commented Nov 3, 2022 at 11:42

1 Answer 1

2

You can combine this into a single call:

.value[] | select(.status == "offline") | "ipAddress: \(.name) id: \(.id)"

Gives:

"ipAddress: ip-172-20-94-68 id: 344"
"ipAddress: ip-172-20-95-119 id: 345"

Online JqPlay Demo

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.