Make Selenium Easy

REST Assured Tutorial 23 – Creating JSON Object Request Body Using Java Map

Last updated on June 29th, 2020 at 10:59 am

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

In previous posts, you must have observed that I was hard coding the JSON request body in a string. It is not a good practice if you have the dynamic payload or want to create payload at run time or parameterized one. It is always good to create payload in such a way that you can easily maintain, manage, update, and retrieve values from it.

We can create a JSON Object using a Map in Java. You must note here that I am using the word “JSON Object”. A JSON Object is a key-value pair and can be easily created using a Java Map. A Map in Java also represents a collection of key-value pairs.

Start with a very simple JSON Object:

Observe above JSON Object. It contains two key-value pairs. “username” and “password” are two keys and “admin” and “password123” are its corresponding values.

Following the same, we need to create a Map and put the above key-value pairs as they ae\re. Since key and value both are string, we can create a generic Map. See the example below:-

Now you can directly pass above Map object to body() method which is overloaded to accept Object type.

Complete example:

I am using a logger just to show you the JSON body. Observe Output below. You will see the request body:-

Now see some complex example:-

Above we have seen a very basic JSON object body. Let’s learn little complex JSON Object request body.

A key in JSON Object can hold another JSON Object as well. Unlike the above example where we created generic Map as Map<String, String>, in this, we need to create a generic map as Map<String, Object> to allow a key in Map to have a JSON Object as value.

We need to create two Maps here. One will hold overall key-value pairs and another Map will hold only bookingdates key-value pairs. Refer example below:-

Complete Example Code:-


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

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 23 – Creating JSON Object Request Body Using Java Map

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