jadupl
jadupl

Reputation: 210

Groovy - JSONSlurper parsing json

I have problem with my SoapUI Groovy script. I have following json (simplified):

{  
"data":{  
  "XXX":[...]
  "YYY":[...]

},
"next":"ffawef234fava23r"
}

I have values of XXX and YYY in my previously TestStep as request parameters and I extract it properly as list of string, but my problem is that I need to extract content of data.XXX and data.YYY, but when I want to do this in loop I always get null. My code:

def content = new JsonSlurper().parseText(response)
def ids = extracted_ids.split(';')     //List of IDs in response above {XXX,YYY}
for (id in ids){
                    log.info id // XXX
                    log.info content.data.'XXX'   //this works 
                    log.info content.data.id      //this not
}

Is there any option to pass this "id" in loop to content.data.{id} to get any content instead of null

Kind Regards

Upvotes: 0

Views: 625

Answers (1)

tim_yates
tim_yates

Reputation: 171094

You just need to do

log.info content.data."$id"

Upvotes: 1

Related Questions