1

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.

3

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.

0

alternatively, you could use a unix utility jtc to walk keyData:

bash $ <file.json jtc -w'[responses][:][keyData]' -r
{ "algorithm": 13, "flags": 257, "protocol": 3, "publicKey": "a1" }
{ "algorithm": 13, "flags": 257, "protocol": 3, "publicKey": "a4" }
{ "algorithm": 13, "flags": 257, "protocol": 3, "publicKey": "fa4" }
bash $ 

and feed it to curl:

bash $ <file.json jtc -w'[responses][:][keyData]' -r | while read keydata; do curl --data "'$keydata'" http://example.com/service ; done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.