top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How To Create Nested JSON Object & Array Payload Using POJO

In this blog, we will explore how to create a POJO payload for a nested JSON object with a JSON array request body.

 

Here, I am creating a Rest Assured API Automation Framework with Cucumber BDD and Maven. We are actually sending the data to the server using the POST request method. Since I am following the Cucumber BDD framework, I need to create a feature file.



We have a  JSON request body here to enter  in the POST request. It has the nested  JSON object and  JSON array. First we need to use the JSON Editor online to check the  JSON object .




Here we have three JSON object

1.Main JSON object,

2.User login object and

3.User Role Maps object.

User Role Map object also has the JSON array in the JSON request body.

 

We need to create POJO classes for each JSON object. So, we will create three POJO classes in our project. "User login" and "User Role Maps" are the nested JSON objects. Therefore, we will first create the nested JSON object class. We will generate the setters and getters for that class by navigating to Eclipse, clicking on "Source," then selecting "Generate Getters and Setters." Next, we'll choose the fields we want to create and click "Generate." Now, we will be able to see some methods inside the class.



Later, we will create the main POJO class object. Here, we will represent the "User Login" and "User Role Maps" POJO class names as the data types of "User Login" and "User Role Maps" in the main POJO object. Since "User Role Maps" has a JSON array, we will declare it using a List in the main POJO class.




Once we have created all three POJO classes, we will proceed to create the setters and getters in the Main POJO class.



After creating all three POJO classes, the project framework will look like this.



Now, we will set the data for the POST request using the setter method from the POJO class in the Resources Package.



Implement Step Definitions: Write step definitions to map the steps in our scenarios to actual test code. In these step definitions, we can use the POJO classes to represent request payloads and perform assertions.



Finally Using the api.CucumberOptions Package ,We will Run the Runner class using Junit Test.



After Running the Junit test we are able to see the Request and Response in Rest assured logger file.


Post Request



Post Response



By following this step, we have created a POJO payload for a nested JSON object with a JSON array request body, and we have also achieved the POST request and response using the POJO payload.


Happy Testing!!!




95 views0 comments

Recent Posts

See All

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
bottom of page