Make Selenium Easy

REST Assured Tutorial 31 – How To Create POJO Classes Of A Nested JSON Payload

As a part of End to End REST Assured Tutorial , in this post We will learn to create a POJO classes for a nested JSON payload.

We already know now :-

  1. To create a POJO for a simple JSON Object Payload
  2. To create a POJO for a simple JSON Array Payload

Now we will make it complex by nesting JSON Object and JSON arrays which we see in real time while working.

Example JSON Payload

Creating POJO classes are simple if we identify what are classes we need to create correctly. A final POJO class for a JSON Payload is created by combining multiple blocks. Let’s identify different objects or POJO :-

There is no need to create a POJO for a 1:1 fields. We can include them in a final POJO class. For example :- In above payload field names like “companyName” , “companyHOCity” and “companyCEO” are 1:1 mapped. Observe value of field name “supportedSalaryBanks“. It is kind of an array of strings which does not need a user defined class to represent specially. Similarly field name “pincodesOfCityOffice” has value as an array of integers. We can define above discussed field as below :-

Now focus on field “employee” which has value as an array of employees. We do not have any ready made data type to represent element of this array as a whole. So here we need to create a POJO class which can contain all details of an employee. Create a simple POJO class to represent below data:-

To represent an array of employees :-

Similar concept for field name “contractors”.

Field name “companyPFDeails” contains value as JSON object which is not present in Java. So we need to create a class for it like Employee above.

Once all building blocks are ready you need to create a final POJO class where all blocks will be kept together to represent whole payload.

Employee POJO Class

Contractors POJO Class

CompanyPFDeails POJO Class

Final POJO class with all building blocks

Let’s create a JSON Payload using above POJO classes.

Output

Note – I have used Jackson Java library to convert a Java object to String. Make sure you have latest dependency added. For this example I have used below maven dependency : –

You can download/clone above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappyLearning

Find all Selenium related post here, all API manual and automation related posts here and find frequently asked Java Programs here.

Many other topics you can navigate through menu.

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

1 thought on “REST Assured Tutorial 31 – How To Create POJO Classes Of A Nested JSON Payload

  1. Hi Amod, Thank you for providing the detail information about Pojo class.I have a doubt about preparing input data for this payload, for example if i want to execute the same test case with different input data then how I can provide the input data from excel sheet , is there any format we need to follow because here we need to provide a lot of data in the array format. if you give this information in detail it will be really helpful for us
    Thank you again

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 31 – How To Create POJO Classes Of A Nested JSON Payload

by Amod Mahajan time to read: 5 min
1
%d bloggers like this: