0

The response from my API is

{
"new": {
    "new1": "value1",
    "new2": "value2"
}
}

When I try to get what is there in jsonPath of "new" using

response.jsonPath().getJsonObject("new").toString()

The above query returns something like below, which is kind of a map

"new1"="value1","new2"="value2"

The response what I look for is,

{
    "new1": "value1",
    "new2": "value2"
}

Tried many other ways as well , but it returns a map only

Melvin Richard
  • 403
  • 1
  • 7
  • 15

1 Answers1

0

JsonPath converts input to map and then works with it. You can try extracting required object/map and then convert it back to JSON string using Jackson's ObjectMapper (or another JSON parser):

Object extracted = response.jsonPath().get("new");
ObjectMapper mapper = new ObjectMapper();
String value = null;
try {
  value = mapper.writeValueAsString(extracted);
} catch (JsonProcessingException e) {
  e.printStackTrace();
}
bhusak
  • 1,320
  • 1
  • 9
  • 19