Using parsed json by JsonSlurper, how do I return a value based on another key value pair of the same node?

100 views Asked by At

I have below JSON and want to fetch the value of "person1" and "person2" either into a map as a key-value pair or individually is also fine.

Expected Output: [attributes:["person1": "ROBERT", "person2": "STEVEN"]]

I started with JSON parsing and dont really have idea on what to do next?

def parsedJSON= new groovy.json.JsonSlurper().parseText(body)

JSON

   "permutationsRequest":{
      "attributes":[
         {
            "name":"person1",
            "value":"ROBERT"
         },
         {
            "name":"person2",
            "value":"STEVEN"
         }
      ]
   }
}
1

There are 1 answers

0
daggett On
def map = parsedJSON.permutationsRequest.attributes.collectEntries{ [it.name,it.value] }
println map.person2