Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 02/19/2025 By admin

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.

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.

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

  com.fasterxml.jackson.core jackson-databind 2.11.0

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.

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.

[
   {
      "firstname":"Jim",
      "lastname":"Brown",
      "totalprice":111,
      "depositpaid":true,
      "additionalneeds":"Breakfast",
      "bookingdates":{
         "checkin":"2021-07-01",
         "checkout":"2021-07-01"
      }
   },
   {
      "firstname" : "Amod",
      "lastname" : "Mahajan",
      "totalprice" : 222,
      "depositpaid" : true,
      "additionalneeds" : "Breakfast",
      "bookingdates" : {
         "checkin" : "2022-07-01",
         "checkout" : "2022-07-01"
     }
  }
]

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

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.

ObjectMapper objectMapper = new ObjectMapper();
                
// Create an array which will hold two JSON objects
ArrayNode parentArray =  objectMapper.createArrayNode();

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

// Creating Node that maps to JSON Object structures in JSON content
                ObjectNode firstBookingDetails = objectMapper.createObjectNode();
                
// It is similar to map put method. put method is overloaded to accept different types of data
// String as field value
firstBookingDetails.put("firstname", "Jim");
firstBookingDetails.put("lastname", "Brown");
// integer as field value
firstBookingDetails.put("totalprice", 111);
// boolean as field value
firstBookingDetails.put("depositpaid", true);
firstBookingDetails.put("additionalneeds", "Breakfast");
                
// Since requirement is to create a nested JSON Object
ObjectNode firstBookingDateDetails = objectMapper.createObjectNode();
firstBookingDateDetails.put("checkin", "2021-07-01");
firstBookingDateDetails.put("checkout", "2021-07-01");
                
// Since 2.4 , put(String fieldName, JsonNode value) is deprecated. So use either set(String fieldName, JsonNode value) or replace(String fieldName, JsonNode value)
firstBookingDetails.set("bookingdates", firstBookingDateDetails);
// Creating Node that maps to JSON Object structures in JSON content
ObjectNode secondBookingDetails = objectMapper.createObjectNode();
// It is similar to map put method. put method is overloaded to accept different types of data
// String as field value
secondBookingDetails.put("firstname", "Amod");
secondBookingDetails.put("lastname", "Mahajan");
// integer as field value
secondBookingDetails.put("totalprice", 222);
// boolean as field value
secondBookingDetails.put("depositpaid", true);
secondBookingDetails.put("additionalneeds", "Breakfast");
                
// Since requirement is to create a nested JSON Object
ObjectNode secondBookingDateDetails = objectMapper.createObjectNode();
secondBookingDateDetails.put("checkin", "2022-07-01");
secondBookingDateDetails.put("checkout", "2022-07-01");
                
// Since 2.4 , put(String fieldName, JsonNode value) is deprecated. So use either set(String fieldName, JsonNode value) or replace(String fieldName, JsonNode value)
secondBookingDetails.set("bookingdates", secondBookingDateDetails);

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

parentArray.add(firstBookingDetails);
parentArray.add(secondBookingDetails);

To add multiple Object Nodes to array at a time we can use addAll() method which accepts a Collection

Uncategorized

Post navigation

Previous Post: API Testing – Postman
Next Post: Part 2: All about XPath: Different ways of writing XPath expression.

Related Posts

Postman Tutorial Part 24 -Set, Get and Unset Global & Environment Variables in Postman Scripts Uncategorized
FrameOutput – Make Selenium Easy Uncategorized
Selenium Topics – Page 9 – Make Selenium Easy Uncategorized
Guidelines To Clear ISTQB Foundation Level Examination Uncategorized
Linux Basic Commands – date Uncategorized
June 26, 2017 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark