JSON stands for JavaScript Object Notation. It’s a standard text-based format that shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application. In order to convert a Java object into JSON, the following two methods can be used that are as listed below as follows:
Java object is converted into JSON using Jackson API.
Steps to Convert Java Object to JSON String
Step 1: Add jar files of Jackson (in the case of the Maven project add Jackson dependencies in the pom.xml file)
html
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
|
Now pom.xml file is depicted below as follows:
File: Geeks/pom.xml

Step 2: Create a POJO (Plain Old Java Object) to be converted into JSON
Java
package com.Geeks;
public class Organisation {
private String organisation_name;
private String description;
private int Employees;
public String getOrganisation_name()
{
return organisation_name;
}
public void
setOrganisation_name(String organisation_name)
{
this.organisation_name = organisation_name;
}
public String getDescription() { return description; }
public void setDescription(String description)
{
this.description = description;
}
public int getEmployees() { return Employees; }
public void setEmployees(int employees)
{
Employees = employees;
}
@Override public String toString()
{
return "Organisation [organisation_name="
+ organisation_name
+ ", description=" + description
+ ", Employees=" + Employees + "]";
}
}
|
Step 3: Create a Java class for converting the Organisation object into JSON.
Convert the object into JSON using ObjectMapper class of Jackson API.
Java
package com.Geeks;
import com.Geeks.Organisation;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
public class ObjectToJson {
public static void main(String[] a)
{
Organisation org = new Organisation();
org = getObjectData(org);
ObjectMapper Obj = new ObjectMapper();
try {
String jsonStr = Obj.writeValueAsString(org);
System.out.println(jsonStr);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static Organisation
getObjectData(Organisation org)
{
org.setName("GeeksforGeeks");
org.setDescription(
"A computer Science portal for Geeks");
org.setEmployees(2000);
return org;
}
|
Step 3: Execute the process.
The output in the JSON will be as below:
Output:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
10 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...