Convert Java Object to JSON String using Jackson API
JSON is most commonly referred to as JavaScript Object Notation. Using JavaScript object syntax, it is a common text format that displays structured data. Data transmission in web applications is frequently done with it. Comparing JSON to other forms, such as XML, it tends to be compact. With object-oriented programming, it is simple to transform JSON files into Java objects. If we want to send data between a web application and a server, JSON is highly recommended.
These two methods, which are given below, can be used to transform a Java object into JSON:
- Jackson API
- GSON
The Jackson API converts a Java object into JSON.
Approach: By using Jackson API
The key component in converting a Java object into JSON is the GSON library. The GSON library can be used to translate a Java object into JSON or the other way around, from a JSON string to an equivalent Java object.
Algorithm:
Step 1: Establish a Maven project.
Step 2: Update the XML file with the GSON dependency.
Step 3: Create an ordinary old Java object and then convert it to JSON.
Step 4: To translate the Java object to JSON, create a Java class.
Implementation:
FileName: jackson-examples/pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javainspires</groupId>
<artifactId>jackson-examples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!--https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.2</version>
</dependency>
</dependencies>
</project>
FileName: MainProgram.java
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.*;
public class MainProgram
{
public static void main(String[] args) throws IOException
{
// Create an instance of the class Employee
Employee employee = new Employee();
// Assigning the properties to the employee object's
employee.setempId(1001L);
employee.setempName("JOHN");
employee.setempAge(35);
employee.setempLocation(new Location(1001L, "Hyderabad"));
// Creating an instance of ObjectMapper for converting objects into JSON
ObjectMapper mapper = new ObjectMapper();
// // Convert the employee object to a JSON string
String empJson = mapper.writeValueAsString(employee);
System.out.println(empJson);
}
}
class Employee
{
private Long empid;
private String empname;
private Integer empage;
private Location emplocation;
public Employee()
{
super();
}
public Employee(Long empid, String empname, Integer empage, Location emplocation)
{
this.empid = empid;
this.empname = empname;
this.empage = empage;
this.emplocation = emplocation;
}
// getters and setters for the properties of the employee object
public Long getempId()
{
return empid;
}
public void setempId(Long empid)
{
this.empid = empid;
}
public String getempName()
{
return empname;
}
public void setempName(String empname)
{
this.empname = empname;
}
public Integer getempAge()
{
return empage;
}
public void setempAge(Integer empage)
{
this.empage = empage;
}
public Location getempLocation()
{
return emplocation;
}
public void setempLocation(Location emplocation)
{
this.emplocation = emplocation;
}
@Override
public String toString()
{
return "Employee [empid=" + empid + ", empname=" + empname + ", empage=" + empage + ", emplocation=" + emplocation + "]";
}
}
class Location
{
private Long empid;
private String empname;
public Location()
{
super();
}
public Location(Long empid, String empname) {
this.empid = empid;
this.empname = empname;
}
// getters and setters for the object's properties
public Long getempId() {
return empid;
}
public void setempId(Long empid) {
this.empid = empid;
}
public String getempName() {
return empname;
}
public void setempName(String empname) {
this.empname = empname;
}
@Override
public String toString() {
return "Location [empid=" + empid + ", empname=" + empname + "]";
}
}
Output:
{"empid":1001,"empname":"JOHN","empage":35,"emplocation":{"empid":1001,"empname":"Hyderabad"}}