top of page

How to send Request Payload for Nested JSON object using POJO?

In this blog, we are going to learn how to send request body for nested JSON object using POJO (Plain Old Java Object). Before going deep into it, let's first see about API testing, Request Body and Response body.

What is API Testing?

API -Application Programming Interface Testing is a software testing practice that tests the APIs directly — from their functionality, reliability, performance, to security. In software application (app) development, API is the middle layer between the presentation (UI) and the database layer. APIs enable communication and data exchange from one software system to another.

What is Request Body?

A request body is data sent by the client to your API. A response body is the data your API sends to the client. The request body is used to send and receive data via the REST API. If we are using POST/PUT API, then based on the REST API contract, we should send the whole resource information because these methods work on the whole resource.

What is JSON?

JSON stands for JavaScript Object Notation. JSON is a human and machine-readable format to represent data as Structured Data. JSON is used primarily to transfer data from one computer to another or even between different programs on the same computer.

Example for JSON format:

Key-Value Pairs:

Key-Value pairs in JSON are used to represent a property of the Object.

Each of these properties have a value associated with it. For e.g First Name has a value of Virender. Similarly, Age has a value of 34. To write a Key-Value in JSON we have to follow these rules:

  • Key-value pairs are separated by a : (colon)

  • Key is always present in Double Quotes " "

  • Value could be anything depending on the Data Type

Object in JSON:

In JSON an object is represented by a collection of Key-Value pairs. This collection of Key-Value pairs are grouped using { } (opening and closing curly braces}. Rules to writing an Object are

  • Key-Value pairs should be separated by a , (Comma)

  • Each Object should Start with an Opening { (Opening Curly Brace)

  • Each Object should End with a Closing } (Closing Curly Brace)

Request body creation:

We can send payload using the following ways.

  •  Hashmap

  •  org.json

  •   POJO (Plain Old Java Object)

  •   External json file

 Nested objects: Objects that contains objects inside them. In the below example we can see the userAddress object is inside the main object.


Sending POST request with the valid URL and request body. In the below request body, userAddress is inside the main object.

Step 1: Here we are using POJO to send payload for nested JSON object and I have used TestNg framework. You can see my framework in the below screen shot.

Step 2: I have added the required dependencies in pom.xml.

Step 3: I have 2 separate POJO class, UserPayload class for main object and UserAddress class for userAddress object. We have Getter and Setter method for each variable seperately. Please find the below screen shots.

POJO class for UserPayload:

POJO class for userAddress:

Step 4: Creating objects for the POJO classes seperately.

// payload objects

public static UserPayload userpayload = new UserPayload();

public static UserAddress userAddress = new UserAddress();

Step 5: Setting the user first name, last name, contact number and email id data values from Excel sheet into main payload object "userpayload".

Step 6: Setting the Plot Number, Street, State, Country and Zipcode data values from Excel sheet to payload object "userAddress" which in turn is set to main object "userpayload".

Step 7: In this step for POST request, in given() part we are setting the Header as ContentType.JSON, authorization as Basic Auth according to the contract and sending the request payload "userpayload" in the body. In when() part we are using rest assured post() method to send the Endpoint of UserAPI. After creating POST request we are storing it in response.

Step 8: Here in the userTest class,

A. we are storing our Base URL in Rest Assured Base URI,

B. then passing the payload into the post request and stored in response.

C. Once the User is created, UserId is generated and we are setting the UserID in userpayload. This UserID wil be used for other request such as GET, PUT and DELETE.

D. We have validated the status code, content Type and schema.


By reading this blog, I hope you might have got an idea on how to send request payload for nested JSON object using POJO and learnt how to write POST request in Rest Assured.

Happy Learning !!

284 views0 comments

Recent Posts

See All


Noté 0 étoile sur 5.
Pas encore de note

Ajouter une note
bottom of page