REST Assured Tutorial 45 – Fetch Value From Nested JSON Object Using JsonNode – Jackson – at() Method

As a part of End to End REST Assured Tutorial, in this post, we will parse a nested JSON objects as JsonNode to fetch values of different types.

Creating POJO classes for parsing a JSON to fetch values may not be easy all the time especially when you have lengthy nested JSON. Instead, we can use the tree structure of a JSON.

Refer basics about JsonNode and parsing a simple JSON object below:-

Fetch Value From JSON Object Using JsonNode – Jackson – Get() & Path() Methods

Since we are using Jackson API of Java for this example, make sure you have the latest dependency of Jackson Databind in your project classpath. I have used below Jackson dependency for this post:-

  com.fasterxml.jackson.core jackson-databind 2.11.1

{
  "firstName": "Amod",
  "lastName": "Mahajan",
  "married": false,
  "salary": 2000.54,
  "addressPin": 45324,
  "skill" :{
    "Java" :"Intermediate",
    "Selenium" :"Intermediate",
    "Javascript" :"Beginner"
  }
}

You can use this site to view the tree representation of a JSON. A tree representation of the above example JSON will look as below:-

A tree structure helps you to navigate to a node easily using its path. We can relate this path as XPath for HTML DOM elements. Ex:- To reach “Java” node we need to opt for path as object(main node)/skill/java.

We need to use class ObjectMapper provided by Jackson API. ObjectMapper class provides a method “readTree()” which is responsible to deserialize JSON content as tree expressed using a set of JsonNode instances.

We can get the value of a node using get() and path() methods of JsonNode class. We need to extract value with appropriate data types after using get() and path() methods.

We have seen the above concept already in the previous post here. In the previous example, we have a simple JSON object with 1 to 1 mapping. For this post, I have taken a nested JSON object as an example where a key contains another JSON object.

We can chain get() or path() methods to traverse to a nested JSON object key as below:-

// Using chaining of get() methods
String javaLevel = jsonTree.get("skills").get("Java").asText();
System.out.println(javaLevel);
                
// Using chaining of path() methods
String javaLevel2 = jsonTree.path("skills").path("Java").asText();
System.out.println(javaLevel2);

This may be longer in the case of a deeply nested JSON object. We can use at() method instead. We need to pass the path of target node similar to a file path.

// Using pattern expression with at()
String javaLevel3 = jsonTree.at("/skills/Java").asText();
System.out.println(javaLevel3);

at() method will never return null if no matching node exists. In that case, it will return a node for which isMissingNode() method returns true.

package JsonNodeJackson;

import org.testng.annotations.Test;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ParseNestedJsonObjectToReadValues {
        
        @Test
        public void parseJsonObjectToReadValues() throws JsonMappingException, JsonProcessingException
        {
                String jsonObject = "{\r\n" + 
                                "  \"firstName\": \"Amod\",\r\n" + 
                                "  \"lastName\": \"Mahajan\",\r\n" + 
                                "  \"married\": false,\r\n" + 
                                "  \"salary\": 2000.54,\r\n" + 
                                "  \"addressPin\": 45324,\r\n" + 
                                "  \"skills\" :{\r\n" + 
                                "    \"Java\" :\"Intermediate\",\r\n" + 
                                "    \"Selenium\" :\"Intermediate\",\r\n" + 
                                "    \"Javascript\" :\"Beginner\"\r\n" + 
                                "  }\r\n" + 
                                "}";
                // Creating an instance of ObjectMapper class
                ObjectMapper objectMapper = new ObjectMapper();
                // Get tree representation of json
                JsonNode jsonTree = objectMapper.readTree(jsonObject);
                
                // Using chaining of get() methods
                String javaLevel = jsonTree.get("skills").get("Java").asText();
                System.out.println(javaLevel);
                
                // Using chaining of path() methods
                String javaLevel2 = jsonTree.path("skills").path("Java").asText();
                System.out.println(javaLevel2);
                
                // Using pattern expression with at()
                String javaLevel3 = jsonTree.at("/skills/Java").asText();
                System.out.println(javaLevel3);
                
        }

}
Intermediate
Intermediate
Intermediate

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning