javarest-assuredrest-assured-jsonpath

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.


Solution

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