Home > Software design >  Jenkins Pipeline: How to iterate in JSON object to get information I want?
Jenkins Pipeline: How to iterate in JSON object to get information I want?

Time:12-18

I am trying to get some properties from docker images stored in Artifactory using AQL. In my Jenkins Pipeline, I am able to hit the aql api, it returns a JSON but I am not able to iterate through the response to get what I need, which is the value of key.IWant.

This is the part of the Groovy code I am using in my Jenkinsfile. As you can see I tried to iterate in 2 different ways, none of them is working.

          // Get the JSON object from the response
          def json = readJSON text: response.content
          println "The follow json obj is ${json}"

          def latestProps = json['results']['properties']
          echo "latestProps ${latestProps}"

          latestPropsRefersTo = ''
          for (def prop in latestProps) {
            echo "Walked through key $prop.key and value $prop.value"
            if(prop.key == 'key.IWant') {
              latestPropsRefersTo = prop.value
              break
            }
          }
          echo "latestPropsRefersTo ${latestPropsRefersTo}"

          def latestPropsRefersTo2 = latestProps.each { key, value ->
            echo "2 - Walked through key $key and value $value"
            if(key == 'key.IWant') {
              return value
            }
          }
          echo "latestPropsRefersTo2 ${latestPropsRefersTo2}"

This is the output I'm getting

Success: Status code 200 is in the accepted range: 100:399
[Pipeline] readJSON
[Pipeline] echo
The follow json obj is [results:[[repo:xxx, path:xxx, name:manifest.json, modified:2021-10-18T19:10:25.062-05:00, properties:[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]], range:[start_pos:0, end_pos:1, total:1]]
[Pipeline] echo
latestProps [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo
Walked through key [key.IWant, key.IDonWant] and value [01, 2021-12-17_23-59-59]
[Pipeline] echo
latestPropsRefersTo 
[Pipeline] echo
2 - Walked through key [[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]] and value null
[Pipeline] echo
latestPropsRefersTo2 [[[key:key.IWant, value:01], [key:key.IDonWant, value:2021-12-17_23-59-59]]]
[Pipeline] echo

How can I make latestPropsRefersTo (or latestPropsRefersTo2) be set to the value 01 correctly? Thanks!

CodePudding user response:

You should be able to use something along those lines to iterate through your json:

// Get the JSON object from the response
def json = readJSON text: response.content
println "The follow json obj is ${json}"               
json.each { myData ->
    myData.properties.each {myProperties ->
          if(myProperties.key == prop.key) {
             latestPropsRefersTo = myProperties.key
           }
    }

CodePudding user response:

              // Get the JSON object from the response
              def json = readJSON text: response.content
              println "The follow json obj is ${json}"
              def latestPropsRefersTo3 = ''
              json.each { results ->
                results.properties.each {prop ->
                  echo "SO Walked3 through key $prop.key and value $prop.value"
                  if(prop.key == 'key.IWant') {
                    latestPropsRefersTo3 = prop.value
                  }
                }
              }
              echo "latestPropsRefersTo3 ${latestPropsRefersTo3}"

This prints:

[Pipeline] echo
The follow json obj is [results:[[repo:xxx, path:xxx, name:manifest.json, modified:2021-12-17T13:59:08.489-06:00, properties:[[key:key.IWant, value:2021-12-17_01-59-04], [key:key.IDonWant, value:113]]]], range:[start_pos:0, end_pos:1, total:1]]
[Pipeline] echo
SO Walked3 through key value and value [[repo:xxx, path:xxx, name:manifest.json, modified:2021-12-17T13:59:08.489-06:00, properties:[[key:key.IWant, value:2021-12-17_01-59-04], [key:key.IDonWant, value:113]]]]
[Pipeline] echo
SO Walked3 through key class and value class java.util.AbstractMap$SimpleImmutableEntry
[Pipeline] echo
SO Walked3 through key key and value results
[Pipeline] echo
SO Walked3 through key value and value [start_pos:0, end_pos:1, total:1]
[Pipeline] echo
SO Walked3 through key class and value class java.util.AbstractMap$SimpleImmutableEntry
[Pipeline] echo
SO Walked3 through key key and value range
[Pipeline] echo
latestPropsRefersTo3

I am still not able to read the information I need from the json object. What else can I try?

  • Related