How to create JSON Array Request Body


In the last tutorial, I have explained  How to test POST JSON Object request using Java Map in Rest Assured . In this tutorial, I will create a Request body using JSON Array in Rest Assured. This request body can be used for POST or PUT operations.

What is JSONArray?

JSONArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array.

  1. JSON array can store multiple value types. The values in a JsonArray can be of the following types: JsonObject, JsonArray, JsonString, JsonNumber, JsonValue.TRUE, JsonValue.FALSE, and JsonValue.NULL.

2. The array index begins with 0.

3. The square brackets [ ] are used to declare JSON array.

An API may accept a JSON Array payload as request body. Imagine, we want to add employee details of more than one employee in the below example. In this case, we can pass multiple JSON objects within a JSON array. I have explain 2 ways to create JSON Object – map or JsonObject. Refer any one of the tutorial to get to know about the creation of JSON Object.

To create JSON Array, we need to add a maven dependency as shown below.

<!-- -->

JSONObject is imported from package:-

import org.json.JSONObject;

JSONaArray is imported from package:-

import org.json.JSONArray;

Below is an example of JSONArray.

How to create JSONArray Request Body or payload?

  1. Create a JSON Object and add the first employee details.
  2. Create another JSON Object and add second guest details
  3. Create a JSONArray.
  4. Add both JSON Object to JSONArray.

Below is an example of creating a request from JSONArray with multiple JSONObjects.  I am using a logger just to print the JSON body in the Console. 

public class RequestBodyAsJSONArrayDemo {

	public void passBodyAsJsonArrayDemo() {

		// JSON Object for first employee
		JSONObject data1 = new JSONObject();

		data1.put("employee_name", "ObjectTest");
		data1.put("profile_image", "test1.png");
		data1.put("employee_age", "30");
		data1.put("employee_salary", "11111");

		// JSON Object for second employee
		JSONObject data2 = new JSONObject();

		data2.put("employee_name", "MapTest");
		data2.put("profile_image", "test2.png");
		data2.put("employee_age", "20");
		data2.put("employee_salary", "99999");

		// Creating JSON array to add both JSON objects
		JSONArray array = new JSONArray();

		// Send the request		
                      .body("message", equalTo("Successfully! Record has been added."))

Below is the execution screenshot.

Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s