Dreessan
Dreessan

Reputation: 11

Trouble using Jackson (JsonNode) to parse nested paths?

Trying to use Jackson to parse JSON. Simple enough- I'm clearly messing up something basic.

final JsonNode root = objectMapper.readTree(res.payload().asUtf8String());
JsonNode body = root.path("body");
JsonNode message = root.path("body").path("errorMessage");
logger.log(Level.INFO, body.asText());
logger.log(Level.INFO, message.asText());

Expected Result:

INFO: {"errorMessage": "input is not a string"}
INFO: "input is not a string"

Actual Result:

INFO: {"errorMessage": "input is not a string"}
INFO: 

Upvotes: 0

Views: 141

Answers (1)

Dreessan
Dreessan

Reputation: 11

Turns out it was because my nested value was, itself, escaped.

debugging root gave something of this form

{
  status: 400,
  headers: {...},
  body: "{\"errorMessage\":\"input is not a string\"}"
}

The strategy then was to use the objectMapper again

final JsonNode root = objectMapper.readTree(res.payload().asUtf8String());
// Body is, itself, escaped so we need to re-parse
JsonNode body = objectMapper.readTree(root.path("body").asText());

Upvotes: 1

Related Questions