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 last post, we have learnt creating JSON Object using Map.

An API may accept a JSON Array payload as well. For example:- Booking for multiple passengers at once. I could not find any example APIs so far for JSON Array but I will update this post as soon as I get.

Suppose an API takes a JSON Array payload as below:-

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

So how can we create such payload?

We already know how to create 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 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.

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 JSON Array.

Example:-

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

You can clone/download example repo here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

You can find all Selenium related post here.
You can find all API manual and automation related posts here.
You can find frequently asked Java Programs here.

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

Leave a Reply