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.
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 :-
- Create an empty JSON Array.
- Create first JSON Object
- Create second JSON Object
- 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