Home > Mobile >  Iterate through the json object and run curl command as variable from json output using jq
Iterate through the json object and run curl command as variable from json output using jq

Time:11-01

I would like to filter the json object while iterating through it and run curl command over each item from the output.

JSON object.

{
  "repo": "releases",
  "path": "/apps/releases",
  "created": "2021-04-01T10:12:23.496-01:00",
  "children": [
    {
      "uri": "/Image1",
      "folder": true,
      "created": 2022-08-09T17.12.22.987.04.000
    },
    {
      "uri": "/Image2",
      "folder": true,
      "created": 2022-06-10T10.12.22.412.10.000
    },
    {
      "uri": "/Image3",
      "folder": true,
      "created": 2022-10-10T07.03.14.742.01.000
    },
    {
      "uri": "/Image4",
      "folder": true,
      "created": 2022-10-10T07.010.11.542.08.000
    }
  ]
}

Looking for some logic that will iterate through the uri under children and that is passed through curl command as $i which would be Image1, Image2 and Image3.

curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/${i}"

While I was running this below command and the output is as follows

for i in $(curl -k -s --user user:password -X GET      "https://artifactory.com/api/releases/baseimage/" | jq -c ".children[] |.uri)

Output: ["/Image1", "/Image2", "/Image3"]

I tried the following command but in the output it replaces ${i} with only Image3, somehow it is not taking Image1 and Image2.

for i in $(curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/" | jq -r ".children[] |.uri); do curl -k -s --user user:password -X GET "https://artifactory.com/api/releases/baseimage/${i}"; done

I tried the following command but in the output it replaces ${i} with only Image3, somehow it is not taking Image1 and Image2.

CodePudding user response:

curl can read URLs to fetch from a file, which you can generate with jq. Something like

base=https://artifactory.com/api/releases/baseimage
curl -k -s --user user:password -X GET "$base/" |
   jq -r --arg b "$base" '.children[].uri | "url = \"\($b)/\(.)\""' |
   curl -k -s --user user:password -X GET --config -

Just one curl process to fetch all the individual images, and no shell loop needed.

CodePudding user response:

You might find it easier to construct the for loop along the following lines:

for uri in $( echo "$json" | jq '.children[].uri') ; do 
  echo curl ... ${uri}...
done   
  • Related