Make Selenium Easy

REST Assured Tutorial 44 – Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() Methods

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will parse a JSON 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

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.

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.

Example Program

Output

Observe above that I used asText(), asBoolean() methods to parse value appropriately. If you are not sure what type of value is held by node then you can use methods like isTextual(), isBoolean(), etc.

You will not get an exception if you are not parsing to correct data types. For example – If you parse a string to boolean using asBoolean() then it works as below as per offical Java doc of Jackson API:-

Method that will try to convert value of this node to a Java boolean. JSON booleans map naturally; integer numbers other than 0 map to true, and 0 maps to false and Strings ‘true’ and ‘false’ map to corresponding values. If representation cannot be converted to a boolean value (including structured types like Objects and Arrays), default value of false will be returned; no exceptions are thrown.

Similarly for asLong():-

Method that will try to convert value of this node to a Java long. Numbers are coerced using default Java rules; booleans convert to 0 (false) and 1 (true), and Strings are parsed using default Java language integer parsing rules. If representation cannot be converted to an long (including structured types like Objects and Arrays), default value of 0 will be returned; no exceptions are thrown.

Retrieving value for a non-existing node

When you are trying to retrieve a value method for a node that doesn’t exist using get() then you will get NullPointerException.

You can use overloaded methods such as asText(String defaultValue), asInt(int defaultValue) to handle these unexpected null values. These methods will handle if a node has explicit value as null. But to use these methods you need to use an alternative of get() method called path() method. With get() if you use above methods then also you will get NullPointerException.

As per the official doc – path() method is similar to get(String), except that instead of returning null if no such value exists (due to this node not being an object, or object not having value for the specified field), a “missing node” will be returned. This allows for convenient and safe chained access via path calls.

The output will be a NullPointerException.

The Output will be “Default value”.

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 44 – Fetch Value From JSON Object Using JsonNode – Jackson – get() & path() Methods

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