RV_Dev RV_Dev - 1 month ago 7
JSON Question

How do I give a where clause or condition to get a value in json response using rest asssured

I need to extract the value of id where name == 'abc'. How can I do that?

here is the example of response:

{
"Text": [
{
"id": "123",
"name": "ABC"
},
{
"id": "456",
"name": "XYZ"
},
{
"id": "789",
"name": "DEF"
}
]
}


So I need to extract the value of id where name =='ABC' should return me id value as 123.
I need to use jayway restassured.

Answer

Use GPath findAll feature

when().
    get("/restapi").
then().
    body("text.findAll{ it.name == 'ABC' }.id", hasItem("123"));
Comments