Forum Discussion

fazlook's avatar
fazlook
Frequent Contributor
8 years ago

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}"

 

4 Replies

  • nmrao's avatar
    nmrao
    Champion Level 3

    fazlook, is any question in there? Or just providing script for others showing an example?

    • fazlook's avatar
      fazlook
      Frequent Contributor

      nmrao yes the question is in my first line :) I would like to know how to get the ID based on a name.

      • nmrao's avatar
        nmrao
        Champion Level 3
        fazlook, would you mind providing raw response? Otherwise, script may not really helpful.