javajsonassertionshamcrestjsonpath

Validate value of specific key which returns list using JsonPath


Response Body:

{
 "Items":[{
            "ID": 12,
            "Name": "n1",
            "Inactive": false
        },
        {
            "ID": 16,
            "Name": "n2",
            "Inactive": false
        },  ...etc
        ]
}

Using JsonPath lib, I was able to get list of all values of specific element (say, "Inactive") with below expression. '$.. Inactive' ---> [false, false,..etc]

But, I am not sure how I can apply matcher using JsonPathAssert for asserting the above list so that it should only contain 'false'.

I am using Java, JsonPath. Can someone pls help me in this?


Solution

  • After some search, I was able to assert list - which should only contain 'false' as mentioned below.

    org.hamcrest.MatcherAssert.assertThat(actualJson.toString(), JsonPathMatchers.hasJsonPath("$..Inactive", Matchers.everyItem(Matchers.equalTo(false))));