Loop into JSON Array in Bash Script

7,186

You could use jq to retrieve the 'keyData' objects, and then pipe it to while read:

jq -c '.responses[].keyData' file.json

{"algorithm":13,"flags":257,"protocol":3,"publicKey":"a1"}
{"algorithm":13,"flags":257,"protocol":3,"publicKey":"a4"}
{"algorithm":13,"flags":257,"protocol":3,"publicKey":"fa4"}

And from there:

jq -c '.responses[].keyData' file.json | 
while read keydata; do curl --data "'$keydata'" http://example.com/service ; done

Putting in your original curl command, the whole pipeline would look like this:

curl -v -X POST --data '{ "authToken": ".......", "clientTransactionId": "",}' https:/domain.tld/api/v1/json/keysList | 
jq -c '.responses[].keyData' file.json | 
while read keydata; do curl --data "'$keydata'" http://example.com/service ; done

Do remember to modify the second curl command with the actual URL, options and so on before execution. If it is necessary, you can add an echo/printf statement before the curl command to see what your request would look like.

Share:
7,186

Related videos on Youtube

Jeff Schaller
Author by

Jeff Schaller

Unix Systems administrator http://www.catb.org/esr/faqs/smart-questions.html http://unix.stackexchange.com/help/how-to-ask http://sscce.org/ http://stackoverflow.com/help/mcve

Updated on September 18, 2022

Comments

  • Jeff Schaller
    Jeff Schaller over 1 year

    I make a curl request to an API and get a json return with jq.

    The result

    {
      "errors": [],
      "metadata": {
        "clientTransactionId": "",
        "serverTransactionId": "20190318164551347"
      },
      "responses": [
        {
          "comment": "",
          "keyData": {
            "algorithm": 13,
            "flags": 257,
            "protocol": 3,
            "publicKey": "a1"
          },
          "keyTag": 28430
        },
        {
          "comment": "",
          "keyData": {
            "algorithm": 13,
            "flags": 257,
            "protocol": 3,
            "publicKey": "a4"
          },
          "keyTag": 28430
        },
        {
          "comment": "",
          "keyData": {
            "algorithm": 13,
            "flags": 257,
            "protocol": 3,
            "publicKey": "fa4"
          },
          "keyTag": 33212
        }
      ],
      "status": "success",
      "warnings": []
    }
    

    Now i want have a loop to make a second api request with the four values frim keyData

    But how can I make it? I search a half day and don't have found out it.

    My request:

    curl -v -X POST --data '{
        "authToken": ".......",
        "clientTransactionId": "",
    }' https:/domain.tld/api/v1/json/keysList | jq .
    

    With jq '.responses[]' I have a "array" with this, but I don't find the solution for a loop with my values.