Access JSON Fields, Arrays, and Nested Objects of JsonNode in Java



What are JSON and JsonNode?

JSON is a format that is used to store data and exchange data between a server and a client. To know more about JSON, you can refer to the JSON Tutorial.

  • A JsonNode is Jackson's tree model for JSON. To read JSON into a JsonNode with Jackson by creating an ObjectMapper instance and call the readValue() method.
  • We can access a field, array or nested object using the get() method of the JsonNode class. We can return a valid string representation using the asText() method and convert the value of the node to a Java int using the asInt() method of JsonNode class.
  • JSON node is a class in the Jackson library that represents a JSON object. It is used for parsing and manipulating JSON data in Java.

In this article, we will learn how to access the JSON fields, arrays, and nested objects of JsonNode in Java.

Accessing the JSON fields, Arrays, & Nested Objects

We will use readTree(), get(), and getArray() Methods of the JsonNode class to access the JSON fields, arrays, and nested objects. We will also use the ObjectMapper class of the Jackson library to parse the JSON string into a JsonNode object.

To use the Jackson library, we need to add the Jackson library to our project. If you are using Maven, add this to your pom.xml file:

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>

If you are not using Maven, you can download the jar file from here.

Steps

The following are the steps to access the JSON fields, arrays, and nested objects of JsonNode in Java:

  • First, import the Jackson library, as discussed above.
  • Then create a JSON string that you want to parse.
  • Then create an instance of ObjectMapper class.
  • Use the readTree method of ObjectMapper class to parse the JSON string into a JsonNode object.
  • Next, use the get method of JsonNode class to access the JSON fields.
  • Then use the get method of JsonNode class to access the JSON arrays.
  • Use the get method of JsonNode class to access the nested JSON objects.
  • Finally, print the values to see how it works.

Example

Following is the code to access the JSON fields, arrays, and nested objects of JsonNode in Java:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

import java.util.Iterator;
import java.util.Map.Entry;

public class AccessFields{
   public static void main(String[] args) throws IOException {
      // Create a JSON string
      String jsonString = "{ "name": "Ansh", "age": 23, "address": { "city": "Delhi", "state": "Delhi" }, "hobbies": ["Reading", "Traveling"] }";

      // Create an ObjectMapper instance
      ObjectMapper objectMapper = new ObjectMapper();

      // Parse the JSON string into a JsonNode object
      JsonNode jsonNode = objectMapper.readTree(jsonString);

      // Access the JSON fields
      String name = jsonNode.get("name").asText();
      int age = jsonNode.get("age").asInt();

      // Access the nested JSON object
      JsonNode addressNode = jsonNode.get("address");
      String city = addressNode.get("city").asText();
      String state = addressNode.get("state").asText();

      // Access the JSON array
      JsonNode hobbiesNode = jsonNode.get("hobbies");
      Iterator<JsonNode> hobbiesIterator = hobbiesNode.elements();
      while (hobbiesIterator.hasNext()) {
         String hobby = hobbiesIterator.next().asText();
         System.out.println("Hobby: " + hobby);
      }

      // Print the values
      System.out.println("Name: " + name);
      System.out.println("Age: " + age);
      System.out.println("City: " + city);
      System.out.println("State: " + state);
   }
}

Following is the output of the above code:

Name: Ansh
Age: 23
City: Delhi
State: Delhi
Hobby: Reading
Hobby: Traveling
Updated on: 2025-05-13T17:15:34+05:30

33K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements