'Getting the value from the response element using GPath and Rest Assured

I want to get the value of status from my response. So that i can assert it. I'm using rest assured with java & serenity BDD.

Response

{
    "locationType": "STORE",
    "locationId": "0003",
    "events": {
        "66e326db-fbfb-4f6e-9d2b-9425e7test5": {
            "status": "BOOKING_OPEN"
        }
    }
}

So, here the event id (66e326db-fbfb-4f6e-9d2b-9425e7test5) is dynamic, which means for each run this UUID will get change.

Code

Response response = SerenityRest.lastResponse();
        final ValidatableResponse validatableResponse = response.then();
        validatableResponse.assertThat().body("events.*.status", containsString(expectedResponse));

When i run this, i'm getting Unrecognized Exception from serenity BDD. I think, that there is some issue in traversing in JSON.
Can someone please help me on getting the value of status here? So in this case, i'm looking for BOOKING_OPEN



Solution 1:[1]

I think you should store UUID as a variable, and change your locator from your response.

response.getBody().jsonPath().get("events."+yourUUID+".status");

Solution 2:[2]

Groovy JsonSlurper does not support * breadthFirst() or ** depthFirst() tokens.

You could use below one to get the String result:

response.getBody().jsonPath().get("events.collect{it.value.status}.find()");
// would return "BOOKING_OPEN"

or below one to get a List result:

response.getBody().jsonPath().get("events.collect{it.value.status}");
//would return ["BOOKING_OPEN"]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joe Phan
Solution 2