Make Selenium Easy

REST Assured Tutorial 33 – De-Serialization – JSON Object To Java Object Using Jackson API

As a part of End to End REST Assured Tutorial, in this post, We will learn the “De-Serialization” concept where we will create Java Object from JSON Objects.

Previously we have learned Serialization – Java Object To JSON Object Using Jackson API.

We have learned about POJOs in the previous posts. If you have not refereed those posts then I will advise you to go through about POJO concepts here:-

What Is Plain Old Java Object (POJO)?

How To Create POJO Classes Of A JSON Object Payload?

How To Create POJO Classes Of A JSON Array Payload?

How To Create POJO Classes Of A Nested JSON Payload?

When we request an API it may return a response body which is mostly JSON or XML. We can use Jsonpath or Xmlpath to traverse through nodes to fetch specific values but it is not advisable if the response is bigger or heavily nested. We can parse that JSON or XML response into POJO classes. After parsing into POJO classes, we can easily get values from response easily. This is called De-serialization. For this, we can use any JSON parser APIs. In this post, we will use Jackosn APIs which is more famous. I will also cover using the GSON library later.

About Jackson API

Jackson API is a high-performance JSON processor for Java. We can perform serialization, deserialization, reading a JSON file, writing a JSON file, and a lot more things using Jackson API.

To use Jackson API, we need to add it to the Java project build path. You can add using Maven or download a jar file with transitive jars.

Let’s first add the latest Jackson Databind dependency in your maven project. I have used below dependency for this post which is the latest version:-

Note:- When we add jackosn-databind dependency then it will automatically download transitive dependencies of the same version i.e. jackson-annotations and jackson-core as well.

If you download and add jackson-databind jar to build path, do not forget to download the other two transitive dependencies as well.

Class ObjectMapper

This is the most powerful class provided by Jackson API and maximum time we will use this class. ObjectMapper class is useful to create JSON object, JSON Array, converting a Java object to a JSON object and vice versa. In this post, we will use the ObjectMapper class to convert a Java Object to a JSON object.

JSON String

We need to parse above JSON string to Java Object. To create a Java object we need a class i.e. one or multiple POJO classes based on the requirement. Let’s create a class with field name exactly (case sensitive) the same as node names in above JSON string because with default setting while parsing JSON object o Java object, it will look on getter setter methods of field names. In fact, access specifiers do not matter here. We will cover some interesting facts on it later.

JSON Sring to POJO

Yup! ObjectMapper class provides various overloaded readValue() methods for different purposes. Reading JSON from a file or as a string which you will see mostly. This method will match fields of JSON string to passed POJO class fields and set values using setter methods which we can easily retrieve using getter methods.

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

2 thoughts on “REST Assured Tutorial 33 – De-Serialization – JSON Object To Java Object Using Jackson API

  1. Good articles Amod.can you please let me know how to handle code when request payload is in xml format but test data passing using json file.wanted to pass testdata in xml payload from json testdata file not though hardcode like admin ,123$ etc. I want value admin and 123$ should come through my framework tetsdata json file.

    Hope you understand my question.I have posted my problem at multiple site where you have provided solution on json DOM parser but didn’t get any response let.Can you please help me here.

    1. You can parse json using any json library such as jackson and read values and use wherever you want.

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 33 – De-Serialization – JSON Object To Java Object Using Jackson API

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