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 :-
Now we will make it complex by nesting JSON Object and JSON arrays which we see in real time while working.
Example JSON Payload
{ "companyName": "MSE", "companyHOCity": "Benagluru", "companyCEO": "Amod", "supportedSalaryBanks": [ "HDFC", "ICICI", "AXIS" ], "pincodesOfCityOffice": [ 560037, 360034, 456343 ], "employee": [ { "firstName": "Amod", "lastName": "Mahajan", "gender": "Male", "age": 28, "salary": 10000.56, "married": false }, { "firstName": "Animesh", "lastName": "Prashant", "gender": "Male", "age": 30, "salary": 20000.56, "married": false }, { "firstName": "Kitty", "lastName": "Gupta", "gender": "Female", "age": 26, "salary": 30000.56, "married": false } ], "contractors": [ { "firstName": "Seema", "lastName": "Singh", "contractFrom": "Jan-2019", "contractTo": "JAN-2025" }, { "firstName": "Hari", "lastName": "Prasad", "contractFrom": "Jan-2017", "contractTo": "JAN-2030" } ], "companyPFDeails": { "pfName": "XYZ", "pfCity": "Benagluru", "pfYear": 2012, "noOfEmployees": 10 } }
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 :-
private String companyName; private String companyHOCity; private String companyCEO; private ListsupportedSalaryBanks; private List pincodesOfCityOffice;
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:-
{ "firstName": "Amod", "lastName": "Mahajan", "gender": "Male", "age": 28, "salary": 10000.56, "married": false }
To represent an array of employees :-
Listemployee;
Similar concept for field name “contractors”.
Listcontractors;
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.
CompanyPFDeails companyPFDeails;
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
package RestfulBookerPojo; public class Employee { // private variables or data members of pojo class private String firstName; private String lastName; private String gender; private int age; private double salary; private boolean married; // Getter and setter methods public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public boolean getMarried() { return married; } public void setMarried(boolean married) { this.married = married; } }
Contractors POJO Class
package RestfulBookerPojo; public class Contractors { private String firstName; private String lastName; private String contractFrom; private String contractTo; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getContractFrom() { return contractFrom; } public void setContractFrom(String contractFrom) { this.contractFrom = contractFrom; } public String getContractTo() { return contractTo; } public void setContractTo(String contractTo) { this.contractTo = contractTo; } }
CompanyPFDeails POJO Class
package RestfulBookerPojo; public class CompanyPFDeails { private String pfName; private String pfCity; private int pfYear; private int noOfEmployees; public String getPfName() { return pfName; } public void setPfName(String pfName) { this.pfName = pfName; } public String getPfCity() { return pfCity; } public void setPfCity(String pfCity) { this.pfCity = pfCity; } public int getPfYear() { return pfYear; } public void setPfYear(int pfYear) { this.pfYear = pfYear; } public int getNoOfEmployees() { return noOfEmployees; } public void setNoOfEmployees(int noOfEmployees) { this.noOfEmployees = noOfEmployees; } }
Final POJO class with all building blocks
package RestfulBookerPojo; import java.util.List; public class NestedPOJO { private String companyName; private String companyHOCity; private String companyCEO; private ListsupportedSalaryBanks; private List pincodesOfCityOffice; List employee; List contractors; CompanyPFDeails companyPFDeails; public String getCompanyName() { return companyName; } public void setCompanyName(String companyName) { this.companyName = companyName; } public String getCompanyHOCity() { return companyHOCity; } public void setCompanyHOCity(String companyHOCity) { this.companyHOCity = companyHOCity; } public String getCompanyCEO() { return companyCEO; } public void setCompanyCEO(String companyCEO) { this.companyCEO = companyCEO; } public List getSupportedSalaryBanks() { return supportedSalaryBanks; } public void setSupportedSalaryBanks(List supportedSalaryBanks) { this.supportedSalaryBanks = supportedSalaryBanks; } public List getPincodesOfCityOffice() { return pincodesOfCityOffice; } public void setPincodesOfCityOffice(List pincodesOfCityOffice) { this.pincodesOfCityOffice = pincodesOfCityOffice; } public List getEmployee() { return employee; } public void setEmployee(List employee) { this.employee = employee; } public List getContractors() { return contractors; } public void setContractors(List contractors) { this.contractors = contractors; } public CompanyPFDeails getCompanyPFDeails() { return companyPFDeails; } public void setCompanyPFDeails(CompanyPFDeails companyPFDeails) { this.companyPFDeails = companyPFDeails; } }
Let’s create a JSON Payload using above POJO classes.
package RestfulBookerPojo; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; public class CreateNestedJSONFromPOJOClasses { public static void main(String[] args) throws JsonProcessingException { NestedPOJO nestedPOJO = new NestedPOJO(); nestedPOJO.setCompanyName("MSE"); nestedPOJO.setCompanyHOCity("Benagluru"); nestedPOJO.setCompanyCEO("Amod"); ListsupportedSalaryBanks = new ArrayList (); supportedSalaryBanks.add("HDFC"); supportedSalaryBanks.add("ICICI"); supportedSalaryBanks.add("AXIS"); nestedPOJO.setSupportedSalaryBanks(supportedSalaryBanks); List pincodesOfCityOffice = new ArrayList (); pincodesOfCityOffice.add(560037); pincodesOfCityOffice.add(360034); pincodesOfCityOffice.add(456343); nestedPOJO.setPincodesOfCityOffice(pincodesOfCityOffice); // Create first employee Employee amod = new Employee(); amod.setFirstName("Amod"); amod.setLastName("Mahajan"); amod.setAge(28); amod.setGender("Male"); amod.setSalary(10000.56); amod.setMarried(false); // Create second employee Employee animesh = new Employee(); animesh.setFirstName("Animesh"); animesh.setLastName("Prashant"); animesh.setAge(30); animesh.setGender("Male"); animesh.setSalary(20000.56); animesh.setMarried(true); // Create third employee Employee kitty = new Employee(); kitty.setFirstName("Kitty"); kitty.setLastName("Gupta"); kitty.setAge(26); kitty.setGender("Female"); kitty.setSalary(30000.56); kitty.setMarried(false); // Creating a List of Employees List allEMployees = new ArrayList (); allEMployees.add(amod); allEMployees.add(animesh); allEMployees.add(kitty); nestedPOJO.setEmployee(allEMployees); Contractors seema = new Contractors(); seema.setFirstName("Seema"); seema.setLastName("Singh"); seema.setContractFrom("Jan-2019"); seema.setContractTo("JAN-2025"); Contractors hari = new Contractors(); hari.setFirstName("Hari"); hari.setLastName("Prasad"); hari.setContractFrom("Jan-2017"); hari.setContractTo("JAN-2030"); List allContractors = new ArrayList (); allContractors.add(seema); allContractors.add(hari); nestedPOJO.setContractors(allContractors); CompanyPFDeails companyPFDeails = new CompanyPFDeails(); companyPFDeails.setPfName("XYZ"); companyPFDeails.setPfCity("Benagluru"); companyPFDeails.setPfYear(2012); companyPFDeails.setNoOfEmployees(10); nestedPOJO.setCompanyPFDeails(companyPFDeails); ObjectMapper objectMapper = new ObjectMapper(); String nestedJsonPayload = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(nestedPOJO); System.out.println(nestedJsonPayload); } }
Output
{ "companyName" : "MSE", "companyHOCity" : "Benagluru", "companyCEO" : "Amod", "supportedSalaryBanks" : [ "HDFC", "ICICI", "AXIS" ], "pincodesOfCityOffice" : [ 560037, 360034, 456343 ], "employee" : [ { "firstName" : "Amod", "lastName" : "Mahajan", "gender" : "Male", "age" : 28, "salary" : 10000.56, "married" : false }, { "firstName" : "Animesh", "lastName" : "Prashant", "gender" : "Male", "age" : 30, "salary" : 20000.56, "married" : true }, { "firstName" : "Kitty", "lastName" : "Gupta", "gender" : "Female", "age" : 26, "salary" : 30000.56, "married" : false } ], "contractors" : [ { "firstName" : "Seema", "lastName" : "Singh", "contractFrom" : "Jan-2019", "contractTo" : "JAN-2025" }, { "firstName" : "Hari", "lastName" : "Prasad", "contractFrom" : "Jan-2017", "contractTo" : "JAN-2030" } ], "companyPFDeails" : { "pfName" : "XYZ", "pfCity" : "Benagluru", "pfYear" : 2012, "noOfEmployees" : 10 } }
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 : –
com.fasterxml.jackson.core jackson-databind 2.11.0
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.
same paylod coming from frontend then how to save it using DTO and entity class
For getting this complex type of information , can you please put a post for that as well, would be interesting to see
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