Search code examples
javarest-assuredrest-assured-jsonpath

Get data from outside the root level of response


I'm still kind of new to REST and haven't been able to figure this one out.

I have a response like this one:

{
    "StatusCode": 200,
    "Result": {
        "CustomerStuff": {
            "Name": "John",
            "State": "Oregon",
            "GetEmail": false
        },
        "eText": "Will only get paper mail. "
    }
}

I would normally save the response body as a string and then use a JsonPath to get what I need.

String responseBody = given().body().when().etc...;
JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result.CustomerStuff");

Then get what I need:

String name = jsonPath.get("name");

I can't figure out how to get the, "eText" value. It's not in the same segment of the response.

Any suggestions?


Solution

  • You should use

    JsonPath jsonPath = new JsonPath(responseBody).setRoot("Result")
    

    And then call jsonPath.get("eText") in order to get the value you want. You can still access CustomerStuff with jsonPath.get("CustomerStuff")