0

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?

Anton
  • 761
  • 17
  • 40
  • I might miss something but can't you simply do `new JsonPath(responseBody).setRoot("Result")` and then call `jsonPath.get("eText")` ? CustomerStuff would be under `jsonPath.get("CustomerStuff")` – lukaszrys Mar 20 '18 at 15:35
  • @ojciecmatki - The ONE thing I didn't try. That seems so obvious now that I feel completely stupid. Thank you. – Anton Mar 20 '18 at 18:43

1 Answers1

1

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")

lukaszrys
  • 1,656
  • 4
  • 19
  • 33