Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find specific key-value pair in an array of object with rest assured

I have a json response something like this:

"someArray": [
    {
       "someProperty":"someValue",
       // other properties that are not relevant for me
    },
    {
       "someProperty":"someOtherValue",
       // other properties that are not relevant for me
    }
]

I want to check, if the someArray array has an object with property named "someProperty" with the value "someValue", but don't fail the test if it has another object with the same property but not the same value.

Is it possible? Until this I was using static index because I had only one element in that array.

like image 377
Hienz Avatar asked Oct 28 '25 07:10

Hienz


1 Answers

Here's a solution using JsonPath:

List<String> values = RestAssured.when().get("/api")
    .then().extract().jsonPath()
    .getList("someArray.someProperty");

Assert.assertTrue(values.contains("someValue"));

Will work for following response JSON:

{  
   "someArray":[  
      {  
         "someProperty":"someValue"
      },
      {  
         "someProperty":"someOtherValue"
      }
   ]
}
like image 103
bhusak Avatar answered Oct 31 '25 01:10

bhusak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!