Make Selenium Easy

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:-

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.

So how can we create such payload?

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.

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.


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

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.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

3 thoughts on “REST Assured Tutorial 24 – Creating JSON Array Request Body Using List

  1. Good to learn like this step by step considering a simple scenario (jsonObject – javamap(string,string) and then thinking about the complex scenario(json object inside json javamap(string,object) and now jsonArray(set<map(string,object) using sets

  2. How to create JSON Object when payload consists both Json Object and Array as well.
    for example if Payload is like this:-
    “resourceType”: “Patient”,
    “id”: “1206675”,
    “meta”: {
    “versionId”: “1”,
    “lastUpdated”: “2020-06-16T07:41:31.812+00:00”,
    “source”: “#iGHDs0yQrKWhYsKX”
    “text”: {
    “status”: “generated”,
    “div”: “Short VICTOR IdentifierCT6151″
    “identifier”: [ {
    “type”: {
    “coding”: [ {
    “system”: “”,
    “code”: “MR”,
    “display”: “Medical Record Number”
    } ],
    “text”: “Medical Record Number”
    “value”: “CT6151”
    }, {
    “type”: {
    “coding”: [ {
    “system”: “”,
    “code”: “NIIP”,
    “display”: “National Insurance Payor Identifier”
    } ],
    “text”: “National Insurance Payor Identifier”
    “value”: “9800001468”
    } ],
    “name”: [ {
    “family”: “Victor”,
    “given”: [ “Short” ]
    } ],
    “gender”: “male”

    1. Hi! Abhishek, you must put JSONObjects into JSONArray and put all into JSONObject
      JSONObject requestParams1 = new JSONObject();
      requestParams1.put(“objectType”, “TravelerRequest”);
      JSONObject requestParams2 = new JSONObject();
      requestParams2.put(“objectType”, “PlaceOfVisit”);
      JSONArray arrData = new JSONArray();
      JSONObject requestParams4 = new JSONObject();
      requestParams4.put(“jsonFullData”, arrData);
      requestParams4.put(“lang”, “en”);

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.

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

by Amod Mahajan time to read: 3 min
%d bloggers like this: