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 List<String> supportedSalaryBanks;
private List<Integer> 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 :-
List<Employee> employee; |
Similar concept for field name “contractors”.
List<Contractors> 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.
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 List<String> supportedSalaryBanks;
private List<Integer> pincodesOfCityOffice;
List<Employee> employee;
List<Contractors> 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<String> getSupportedSalaryBanks() {
return supportedSalaryBanks;
}
public void setSupportedSalaryBanks(List<String> supportedSalaryBanks) {
this.supportedSalaryBanks = supportedSalaryBanks;
}
public List<Integer> getPincodesOfCityOffice() {
return pincodesOfCityOffice;
}
public void setPincodesOfCityOffice(List<Integer> pincodesOfCityOffice) {
this.pincodesOfCityOffice = pincodesOfCityOffice;
}
public List<Employee> getEmployee() {
return employee;
}
public void setEmployee(List<Employee> employee) {
this.employee = employee;
}
public List<Contractors> getContractors() {
return contractors;
}
public void setContractors(List<Contractors> 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");
List<String> supportedSalaryBanks = new ArrayList<String>();
supportedSalaryBanks.add("HDFC");
supportedSalaryBanks.add("ICICI");
supportedSalaryBanks.add("AXIS");
nestedPOJO.setSupportedSalaryBanks(supportedSalaryBanks);
List<Integer> pincodesOfCityOffice = new ArrayList<Integer>();
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<Employee> allEMployees = new ArrayList<Employee>();
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<Contractors> allContractors = new ArrayList<Contractors>();
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
}
}