Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 24 – Creating JSON Array Request Body Using List

Posted on 02/19/2025 By admin

As a part of End to End REST Assured Tutorial, in this post We will learn:- How to create a JSON Array Payload using List in Java.

In the last post, we have learned creating JSON Object using Map.

An API may accept a JSON Array payload as well. For example:- Booking for multiple passengers at once. In this case, we may need to pass multiple JSON objects within a JSON array. An example is below:-

[
    {
        "firstname": "Amod",
        "additionalneeds": "Breakfast",
        "bookingdates": {
            "checkin": "2021-08-01",
            "checkout": "2021-08-02"
        },
        "totalprice": 222,
        "depositpaid": true,
        "lastname": "Mahajan"
    },
    {
        "firstname": "Animesh",
        "additionalneeds": "Breakfast",
        "bookingdates": {
            "checkin": "2021-07-01",
            "checkout": "2021-07-01"
        },
        "totalprice": 111,
        "depositpaid": true,
        "lastname": "Prashant"
    }
]

I just twisted Restful Booking API for multiple bookings at once. We need to add as many JSON Objects containing guest details as required in a JSON Array. For example:- I want to do two bookings at once so added two booking details.

We already know how to create a JSON Object using Map. A JSON Array in Java can be created using List or Set. So we need to perform below steps to create payload as above:-

  1. Create a JSON Object and add the first guest details.
  2. Create another JSON Object and add second guest details
  3. Create a List or Set object.
  4. Add both JSON Object to List.
// JSON Object for first guest
Map bookingOne = new HashMap();
bookingOne.put("firstname", "Amod");
bookingOne.put("lastname", "Mahajan");
bookingOne.put("totalprice", 222);
bookingOne.put("depositpaid", true); Map bookingDatesMapForAmod = new HashMap();
bookingDatesMapForAmod.put("checkin", "2021-08-01");
bookingDatesMapForAmod.put("checkout", "2021-08-02"); bookingOne.put("bookingdates", bookingDatesMapForAmod);
bookingOne.put("additionalneeds", "Breakfast"); // JSON Object for second guest
Map bookingTwo = new HashMap();
bookingTwo.put("firstname", "Animesh");
bookingTwo.put("lastname", "Prashant");
bookingTwo.put("totalprice", 111);
bookingTwo.put("depositpaid", true); Map bookingDatesMapForAnimesh = new HashMap();
bookingDatesMapForAnimesh.put("checkin", "2021-07-01");
bookingDatesMapForAnimesh.put("checkout", "2021-07-01"); bookingTwo.put("bookingdates", bookingDatesMapForAnimesh);
bookingTwo.put("additionalneeds", "Breakfast"); // Creating JSON array to add both JSON objects
List> jsonArrayPayload = new ArrayList(); jsonArrayPayload.add(bookingOne);
jsonArrayPayload.add(bookingTwo); 

As Booking API does not accept JSON Array payload, but to show as an example, I am just passing it. Because of invalid payload, it is giving internal server error. You can ignore that and focus on creating a JSON Array.

package DifferentWaysOfPassingPayloadToRequest; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.testng.annotations.Test; import io.restassured.RestAssured;
import io.restassured.http.ContentType; public class CreatingNestedJsonArray { @Test public void CreatingNestedJsonObjectTest() { // JSON Object for first guest Map bookingOne = new HashMap(); bookingOne.put("firstname", "Amod"); bookingOne.put("lastname", "Mahajan"); bookingOne.put("totalprice", 222); bookingOne.put("depositpaid", true); Map bookingDatesMapForAmod = new HashMap(); bookingDatesMapForAmod.put("checkin", "2021-08-01"); bookingDatesMapForAmod.put("checkout", "2021-08-02"); bookingOne.put("bookingdates", bookingDatesMapForAmod); bookingOne.put("additionalneeds", "Breakfast"); // JSON Object for second guest Map bookingTwo = new HashMap(); bookingTwo.put("firstname", "Animesh"); bookingTwo.put("lastname", "Prashant"); bookingTwo.put("totalprice", 111); bookingTwo.put("depositpaid", true); Map bookingDatesMapForAnimesh = new HashMap(); bookingDatesMapForAnimesh.put("checkin", "2021-07-01"); bookingDatesMapForAnimesh.put("checkout", "2021-07-01"); bookingTwo.put("bookingdates", bookingDatesMapForAnimesh); bookingTwo.put("additionalneeds", "Breakfast"); // Creating JSON array to add both JSON objects List> jsonArrayPayload = new ArrayList(); jsonArrayPayload.add(bookingOne); jsonArrayPayload.add(bookingTwo); //GIVEN RestAssured .given() .baseUri("https://restful-booker.herokuapp.com/booking") .contentType(ContentType.JSON) .body(jsonArrayPayload) .log() .all() // WHEN .when() .post() // THEN .then() .assertThat() // Asserting status code as 500 as it does not accept json array payload .statusCode(500) .log() .all(); } } 

You can see passing JSON Array body in the output below:-

[RemoteTestNG] detected TestNG version 7.0.0
Request method: POST
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Accept=*/* Content-Type=application/json; charset=UTF-8
Cookies: 
Multiparts: 
Body:
[ { "firstname": "Amod", "additionalneeds": "Breakfast", "bookingdates": { "checkin": "2021-08-01", "checkout": "2021-08-02" }, "totalprice": 222, "depositpaid": true, "lastname": "Mahajan" }, { "firstname": "Animesh", "additionalneeds": "Breakfast", "bookingdates": { "checkin": "2021-07-01", "checkout": "2021-07-01" }, "totalprice": 111, "depositpaid": true, "lastname": "Prashant" }
]
HTTP/1.1 500 Internal Server Error
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: text/plain; charset=utf-8
Content-Length: 21
Etag: W/"15-/6VXivhc2MKdLfIkLcUE47K6aH0"
Date: Thu, 27 Feb 2020 17:21:58 GMT
Via: 1.1 vegur Internal Server Error
PASSED: CreatingNestedJsonObjectTest =============================================== Default test Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
=============================================== 

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

Uncategorized

Post navigation

Previous Post: #3. Generic Interface In Java
Next Post: Part 1: Usages Of Javascripts In Selenium: Why We Need Javascript Commands In Selenium

Related Posts

How To Iterate Map In Java? Uncategorized
image – Make Selenium Easy Uncategorized
April 30, 2018 – Make Selenium Easy Uncategorized
How Much Java Required For Selenium? Uncategorized
api testing tutorials Uncategorized
Postman Tutorial Part 35 – Extracting Value From JSON Array Response in Postman – JSON Array Parsing in Postman 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