Make Selenium Easy

REST Assured Tutorial 27 – How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()

As a part of End to End REST Assured Tutorial , in this post We will learn to Create JSON Array using Jackson API’s class ObjectMapper.

We have already learnt to create JSON Object using Jackson API here.

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 Array or ArrayNode.

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

Above JSON payload is a JSON array which holds two JSON Objects. Out approach will be :-

  1. Create an empty JSON Array.
  2. Create first JSON Object
  3. Create second JSON Object
  4. Add created JSON Objects to JSON Array

Create an empty JSON Array

To create a JSON Object we used createObjectNode() method of ObjectMapper class. Similarly to create JSON Array we use createArrayNode() method of ObjectMapper class. createArrayNode() will return reference of ArrayNode class.

Note :- Return type of createArrayNode() or createObjectNode() is co-variant. We will see this concept later.

Create first JSON Object

We already know how to create JSON object using Jackson API.

Create second JSON Object

Add created JSON Objects to JSON Array

Class ArrayNode provides a method add() which can add one JSON Object to array at a time.

To add multiple Object Nodes to array at a time we can use addAll() method which accepts a Collection<? extends JsonNode> . We can create a List (because List extends Collection)of all JSON Objects and add.

Note:- If you try to add duplicate Object Nodes, it will be added to array.

To print created JSON array as string with proper format

That’s all. We have created a JSON Array successfully using Jackson API.

Let’s learn some retrieving and manipulation methods.

Retrieving JSON Object from JSON array using index

Since JSON Array is iterable, we can retrieve a JSON object using its index.

Get size of JSON Array

size() method can be used to get size of JSON Array.

Iterate JSON Array

iterator() method can be used to iterate through a JSON Array.

Remove a JSON Object from JSON Array

We can remove a JSON object from JSON Array using its index.

Empty JSON Array

Complete Program


Passing ArrayNode or JSON Array to request is similar to ObjecNode or JSON object which we have learnt here.

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.

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

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.

2 thoughts on “REST Assured Tutorial 27 – How To Create JSON Array Using Jackson API – ObjectMapper – CreateArrayNode()

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.