Make Selenium Easy

REST Assured Tutorial 47 – Fetch Value From Nested JSON Array Using JsonNode – Jackson – At() Method

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will parse a JSON Array 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 and dynamic JSON. Instead, we can use the tree structure of a JSON so that we can navigate to any node via a path.

Prerequisite

We have already covered parsing a simple JSON Object, Nested JSON Object and simple JSON Array as JsonNode previously. You must refer those posts to understand parsing nested JSON array well.

Fetch Value From JSON Object Using JsonNode

Fetch Value From Nested JSON Object Using JsonNode

Fetch Value From JSON Array Using JsonNode

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 Array

Example JSON Array

Tree representation of JSON Array

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

A JSON array may be a collection of JSON objects or JSON arrays. Below is also a valid example of a JSON array.

Deserialize a JSON Array 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 just need to use an index to fetch an element of an array which is the core concept of an array. We have already seen this concept in detail here.

For this post, I have taken a nested JSON Array as an example. If I need to get value of the “type” node of first JSON object from JSON array, we need to write statement as below:-

Above statement may be longer or complex if JSON object or JSON array is deeply nested. We can use at() method instead. We need to pass the path of target node similar to a file path.

Above you see “0” and “1” they represent indexes. In fact, it is intelligent enough to use those numbers as index or key. If the node key is a number such as “0” or “1” etc then it will not complain. First, it will try to look for an index. If it is not a JSON array then it will look for a node key. If both are not present then only it will fail.

Example Program

Output

You can play around below JSON array which contains keys as numbers.

The below statements will give you the value of nodes:-

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

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 47 – Fetch Value From Nested JSON Array Using JsonNode – Jackson – At() Method

by Amod Mahajan time to read: 4 min
0
%d bloggers like this: