0

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.

nick
  • 23
  • 4
  • 1
    Please fix the sample JSON object, it's not valid json – glenn jackman Oct 31 '22 at 17:21
  • 1
    But you probably want `jq -r '.children[].uri'` – glenn jackman Oct 31 '22 at 17:23
  • Please use 3 backticks around a code block. Your question is getting unreadable – 0stone0 Oct 31 '22 at 17:26
  • For an array containing all `uri`: [`jq '.children | map(.uri)'`](https://jqplay.org/s/nWsAz0euwTW) – 0stone0 Oct 31 '22 at 17:27
  • 1
    Does this answer your question? [How to extract a field from each object in an array with jq?](https://stackoverflow.com/questions/48426052/how-to-extract-a-field-from-each-object-in-an-array-with-jq) – 0stone0 Oct 31 '22 at 17:27
  • Thank you, I just updated the question. The problem is I was able to retrieve uri as ["/Image1","/Image2","/Image3"] but when I tried to iterate through these values using curl command it fails. 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 – nick Oct 31 '22 at 17:35

2 Answers2

0

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   
peak
  • 105,803
  • 17
  • 152
  • 177
0

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Hi @chepner, is it possible to get the output as key value pair and sorted by time ["Image4": "2022-10-10T07.010.11.542.08.000", "Image3": "2022-10-10T07.03.14.742.01.000", "Image1": "2022-08-09T17.12.22.987.04.000", "Image2": "2022-06-10T10.12.22.412.10.000"] – nick Oct 31 '22 at 22:03