I have following groovy script to get the values from the response.
import com.eviware.soapui.support.XmlHolder
import groovy.json.JsonSlurper
def response = context.expand( '${GetLoansList#Response}' ).toString()
log.info(response)
def slurper = new JsonSlurper()
def json = slurper.parseText response
log.info(json.items.id)
my json response is similar to this
{
"items" : [
{
"id" : 48223,
"name" : "LAI-00151007",
"amount" : 25050.0,
"interest_rate" : 25.99,
"term" : 60,
},
{
"id" : 48262,
"name" : "LAI-00152581",
"amount" : 44225.0,
"interest_rate" : 18.9,
"term" : 36,
},
],
"total_count" : 13
}
I want to get the corresponding 'id' for the given name ("name": "LAI-00152581",). What is the best way to do this? Thanks
You can use:
json.items.find({ it.name == "LAI-00152581" })?.id
?.
is for safety when there is no items
meeting the criteria. In which case the result will be null
Since Groovy 2.5.0 there is one more way of doing this, which is semantically equivalent:
json.items.findResult { if (it.name == "LAI-00152581") return it.id }