Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

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

Uncategorized

Post navigation

Previous Post: Test Automation Framework – A Platform To Develop Test Scripts
Next Post: Frequently Asked Java Programs: 34 – Java Program to Check if Two Strings are Anagram

Related Posts

nodejs Uncategorized
image – Make Selenium Easy Uncategorized
position in xpath Uncategorized
July 2018 – Make Selenium Easy Uncategorized
API Testing – Page 3 Uncategorized
September 29, 2019 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark