fazlook
8 years agoFrequent Contributor
Get an ID based on a name using Script Assertion/JSON
How to get and ID from a response based on a name ?
ex:
{
"id": "1",
"name" = "Tom"
}
{
"id": "2",
"name": "Jerry"
}
the following did not work:
//Get the ID
def id = names.find {it.name == "Jerry"}.id
log.info "id from response is ${id}"
I did this :
assert context.response, "Response is empty or null"
def jsonObject = new groovy.json.JsonSlurper().parseText(context.response)
def names = jsonObject.name
def id = jsonObject.id
for ( name in names)
{
if (name == 'Jerry')
{
log.info id;
}
It is printing both ID's.
}
//Get the ID
//def id = names.find {it == "Jerry"}.id
//log.info "id from response is ${id}"