Make Selenium Easy

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

Last updated on July 6th, 2020 at 11:18 am

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.

Example:-

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.
#ThanksForReading
#HappyLearning

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

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

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
0
%d bloggers like this: