REST Assured Tutorial 24 – Creating JSON Array Request Body Using List
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:-
- Create a JSON Object and add the first guest details.
- Create another JSON Object and add second guest details
- Create a List or Set object.
- 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