Make Selenium Easy

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

Introduction

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.

Prerequisite

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:-

Tree representation of JSON Object

Example JSON Object

Tree Structure

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.

Deserialize a JSON Object to Tree

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:-

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.

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.

Example Program

Output

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
1
%d bloggers like this: