Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to read a nested value in a JSON using Jackson

I have a json structure like this:

{
  "version": 1.0,
  "object": {
    "a1": "a2",
    "b1": "b2
  },
  "deploy": {
    "applicationName": "app",
    "namespace": "com.abc.xyz"
    ...
  }
}

The ... means the JSON continues more. I want to retrieve the namespace from this JSON. My code looks like this:

JsonNode rootNode = new ObjectMapper().readTree(json);
var result = rootNode.at("/deploy/namespace");

However, with this code, result will always ""

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried different paths but I’m always getting an empty String.
Any help?

>Solution :

The problem is that at return a JsonNode, so you need to access it:

JsonNode rootNode = new ObjectMapper().readTree(json);
var result = rootNode.at("/deploy/namespace").asText();

Otherwise, you can also access it in this way:

JsonNode rootNode = new ObjectMapper().readTree(json);
var result = rootNode.path("deploy").path("namespace").asText();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading