In the last tutorial, I explained How to test POST Request using Rest Assured where the request body is in String Format. In this tutorial, I will create a request body using JSON Object in Rest Assured. This request body can be used for POST or PUT operations.
First, set up a basic Rest Assured Maven Project by clicking here and Gradle project by clicking here.
In the previous post, you must have observed that I had hard-coded the JSON request body in a string format. It is not a good practice if you have a dynamic payload or want to create a payload at run time or parameterized one. It is always recommended to create a payload in such a way that you can easily maintain, manage, update, and retrieve values from it. This can be achieved by JSONObject or JSONArray.
A JSONObject is an unordered collection of key and value pairs, resembling Java’s native Map implementations. JSON stores data as a key-value pair. The key is the left side and the value is the right side, and a semicolon is used to separate both. One key-value pair is separated from another key-value pair using a comma (,).
The internal form is an object having the get and opt methods for accessing the values by name and put methods for adding or replacing values by name. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or JSONObject.NULL object.
To create a request body using JSON Object, we need to add a maven dependency.
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20210307</version>
</dependency>
JSONObject is imported from package:-
import org.json.JSONObject;
JSONObject exposes an API similar to Java’s Map interface. Below is an example of creating a request from a JSON Object. Here, Id is the auto-generated attribute. We are using the put() method and supply the key and value as an argument. I am using a logger just to print the JSON body in the Console.
@Test
public void passBodyAsJsonObject() {
JSONObject data = new JSONObject();
data.put("employee_name", "MapTest");
data.put("profile_image", "test.png");
data.put("employee_age", "30");
data.put("employee_salary", "11111");
RestAssured
.given()
.contentType(ContentType.JSON)
.body(data.toString())
.log().all()
.when()
.post("http://dummy.restapiexample.com/api/v1/create")
.then()
.assertThat().statusCode(200)
.body("data.employee_name", equalTo("MapTest"))
.body("data.employee_age", equalTo("30"))
.body("data.employee_salary", equalTo("11111"))
.body("data.profile_image", equalTo("test.png"))
.body("message", equalTo("Successfully! Record has been added."))
.log().all();
}
The Request body will look as shown below:-

The texts produced by the toString() methods strictly conform to the JSON syntax rules.
.body(data.toString())
The above one is a simple JSON Request Body. Let’s take an example of a Complex Request Body or nested Request Body.

We need to create two JSONObject here. One will hold overall key-value pairs, and another JSONObject will hold only employee_details key-value pairs.
Below is an example that shows how to create a complex JSON request body using JSONObject.
@Test
public void passBodyAsJsonObjectDemo() {
//First JSONObject
JSONObject data = new JSONObject();
data.put("profile_image", "test.png");
//Second JSONObject
JSONObject detail = new JSONObject();
detail.put("updated_message", "Details of New Resource");
detail.put("employee_age", "30");
data.put("employee_details", detail);
data.put("employee_name", "MapTest");
data.put("employee_salary", "11111");
RestAssured
.given()
.contentType(ContentType.JSON)
.body(data.toString())
.log().all()
.when()
.post("http://dummy.restapiexample.com/api/v1/create")
.then()
.assertThat().statusCode(200)
.body("data.employee_name", equalTo("MapTest"))
.body("data.employee_details.employee_age", equalTo("30"))
.body("data.employee_details.updated_message", equalTo("Details of New Resource"))
.body("data.employee_salary", equalTo("11111")).body("data.profile_image", equalTo("test.png"))
.body("message", equalTo("Successfully! Record has been added."))
.log().all();
}
}
The Request body will look as shown below:-

We are done. Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!