Make Selenium Easy

REST Assured Tutorial 25 – How To Create a JSON Object Using Jackson API – ObjectMapper – createObjectNode()

We already learnt Creating JSON Object Request Body Using Map. In this post we will learn to Create a JSON Object or Object Node ( In terms of Jackson API ) using Jackson API.

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 in java project build path. You can add using Maven or download a jar file with transitive jars.

Maven Dependency

Always try to use latest dependency from Central Maven Repository. I will use below dependency at the time of writing this post.

Note :- When we add jackosn-databind dependency then it will automatically download transitive dependencies of 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 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. As of now just know that ObjectMapper provides functionalities for reading and writing JSON. We will learn more about it as we proceed further.

We will use Class ObjectMapper to create a JSON Object or ObjectNode.

Let’s start with our familiar JSON payload which we will create using ObjectMapper.

Already I have explained that above JSON is a nested JSON Object. One JSON object is root node which holds fields like firstname, lastname, totalprice, depositpaid,additionalneeds and bookingdates where bookingdates is another JSON object.

Create a JSON Object or Object Node

To create a JSON Object using Jackson, we need to use createObjectNode() method of ObjectMapper class which returns an ObjectNode class instance. ObjectNode class has overloaded methods put(String fieldName, T fieldValue ) which takes field Name as String and values of different primitive and wrapper class types like String, Boolean etc. All field names should be unique. If you pass duplicate field name, it will not throw any error but override field values by latest. It is similar to put() method of Map in Java.

To get the created JSON Object as string, use writeValueAsString() provided by ObjectMapper class. If you want in proper JSON format, use writerWithDefaultPrettyPrinter() method for formatting.

Example Code

Output :

Create a nested JSON Object or Object Node

To create a nested JSON Object or put another JSON Object as field value, we can not use put(String fieldName, JsonNode fieldValue) as it is deprecated. We use set(String fieldName, JsonNode fieldValue) or replace(String fieldName, JsonNode fieldValue)

Example Code

Output

We will see some manipulation methods which are useful in parsing created JSON Object. We can use these methods to validate values. For example whatever booking details we passed, same should be used for booking.

Retrieve a field value from JSON Object or ObjectNode

To retrieve a field value we need to use get(String fieldName). If passed field name does not have a value or if there is no field with such name, null is returned. It returns a JsonNode. To get value in actual data types we need to use respective methods like asText() to get value as String or asBoolean() to get value as boolean. Be careful when field value is another ObjectNode.

Example Code

Output

Retrieve all field names from JSON Object or Object Node

To retrieve all field names from a ObjectNode, we need to use fieldNames() methods which returns an Iterator<String>. To get count of fields in an ObjectNode, we can use size() method.

Example Code

Output

Retrieve all field values from from JSON Object or ObjectNode

To retrieve all field values from an ObjectNode, use elements() method which returns an Iterator of JsonNode.

Output

Retrieve all key-value pair from JSON Object or ObjectNode

We can use fields() method to get all fields (with both names and values) of a JSON Object. It returns an Iterator<Entry<String,JsonNode>>.

Output

Remove a field from JSON Object or ObjectNode

Use remove(String fieldName) method to remove a field from ObjectNode. It will return value of the field, if such field existed; null if not.

Example Code

Output

Update a field from JSON Object or ObjectNode

We need to use put() method to update a field value if fieldValue is not another ObjectNode. If fieldValue is an ObjectNode use set() or replace() method.

Example Code

Output

Complete Code

Output

There are many methods in ObjectNode which you can explore. We mostly use above methods in API automation. In next post we will learn to use created ObjectNode in Rest Assured.

You can download/clone 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 post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through 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 25 – How To Create a JSON Object Using Jackson API – ObjectMapper – createObjectNode()

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