Make Selenium Easy

REST Assured Tutorial 32 – Serialization – Java Object To JSON Object Using Jackson API

As a part of End to End REST Assured Tutorial, in this post, We will learn the “Serializaion” concept where we will create JSON Object or Payload from Java Objects for APIs.

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?

Now we will learn how to create JSON objects using POJOs. To create a JSON object (JSON) from Java Objects( POJOs) is called 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.

Target JSON Object to be created:-

POJO Class

Let’s create a class with field name exactly (case sensitive) the same as node names in above JSON payload because with default setting while parsing Java Object to JSON 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.

POJO to JSON String

There are many ways or methods available in the ObjectMapper class. Below is a glimpse:-

You can write Java object as a string or byte array or into a file etc. For API we mostly used as a string or into a file. For proper formating use “writerWithDefaultPrettyPrinter()” before writing. Let’s create an object of MSE_EmployeePojo i.e. Java Object and write it as a JSON object.

Output:-

POJO to a JSON File

We just need to pass the target JSON file path and use the overloaded writeValue(File file, Object obj) method.

Refresh project folder if not auto refreshed (In case of Eclipse IDE). Navigate to the path you have provided.

You can see JSON details as below:-

Complete Code

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

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “REST Assured Tutorial 32 – Serialization – Java Object To JSON Object Using Jackson API

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.