Search code examples
rest-assuredrest-assured-jsonpath

JsonPath syntax for maps


Let's say i have a web service returning this kind of payload

{
    "50": [
        {
            "temperature": 7,
            "day": "2023-01-02"
        },
        {
            "temperature": 6,
            "day": "2023-01-01"
        }
    ],
    "51": [
        {
            "temperature": 8,
            "day": "2023-01-01"
        }
    ]
}

The response contains as many physical devices' data as found in a DB. Here, there are 2, with ids "50", "51".

In a RestAssured test, I can validate precise items of the body with this snippet :

.body("'50'.size()", Is(2))
.body("'50'[0].temperature", Is(7))

But I'd also like to validate, that this body contains 2 ids, and not 1 or 3,... without knowing their exact values.

I can't find the correct syntax to validate this assumption. Any idea ?


Solution

  • The syntax is

    .body("keySet().size()", Is(2))
    

    keySet() will return a Set of key, then .size() will return the size of the Set.