Google has developed a JSON library for Java called Gson. We can create JSON and translate it into Java objects using Gson. Gson can output JSON in compact format by default. The setPrettyPrinting() function of the GsonBuilder class must be used to set up the Gson instance in order to enable pretty printing. This method tells Gson to produce JSON that fits on a page for pretty printing.
Add the below dependency to POM.xml to use Gson API.
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
Let us take an example of a JSON.
{
"firstName" : "Vibha",
"lastName" : "Singh",
"age" : 30,
"salary" : 75000.0,
"designation" : "Manager",
"contactNumber" : "+919999988822",
"emailId" : "abc@test.com"
}
Let us create a table named Employee which contains the data members same as node names in the above JSON payload and their corresponding getter and setter methods.
public class Employee {
// private data members of POJO class
private String firstName;
private String lastName;
private int age;
private double salary;
private String designation;
private String contactNumber;
private String emailId;
// 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 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 String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
}
We will convert a Java Object to a JSON object as a String and also will write it into a .json file. There are many variations for the method toJson().
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-70.png?w=300)
You can create a Gson instance by invoking a new Gson() if the default configuration is all you need, as shown in the below example.
@Test
public void withoutPretty() {
// Create an object of POJO class
Employee employee = new Employee();
employee.setFirstName("Vibha");
employee.setLastName("Singh");
employee.setAge(30);
employee.setSalary(75000);
employee.setDesignation("Manager");
employee.setContactNumber("+919999988822");
employee.setEmailId("abc@test.com");
Gson gson = new Gson();
String employeeJsonPayload = gson.toJson(employee);
System.out.println("Json :" + employeeJsonPayload);
}
The execution message is shown below.
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-71.png?w=1200)
Syntax
public GsonBuilder setPrettyPrinting()
Below is the program to print the JSON in pretty format.
@Test
public void withPretty() {
// Create an object of POJO class
Employee employee = new Employee();
employee.setFirstName("Vibha");
employee.setLastName("Singh");
employee.setAge(30);
employee.setSalary(75000);
employee.setDesignation("Manager");
employee.setContactNumber("+919999988822");
employee.setEmailId("abc@test.com");
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String json = gson.toJson(employee);
System.out.println("Pretty Json :" + json);
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-72.png?w=469)
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!