Make Selenium Easy

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

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 JSON request body in a string. It is not a good practice if you have dynamic payload or want to create payload at run time or parameterised 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 word “JSON Object”. A JSON Object is a key – value pair and can be easily created using a 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 above key-value pairs. 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 logger just to show you JSON body. Observe Output below. You will see 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 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:-

Output:-

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

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 Map

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