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 a web application. To convert a Java object into JSON, the following two methods can be used, that are as listed below as follows:
- GSON
- JACKSON API
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)
<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
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) {
this.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.
Use the ObjectMapper class provided by Jackson to convert the Java object into a JSON string.
package com.Geeks;
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectToJson {
public static void main(String[] args) {
// Creating Organisation object
Organisation org = getObjectData();
// Creating ObjectMapper instance
ObjectMapper mapper = new ObjectMapper();
try {
// Converting Java object to JSON string
String jsonString = mapper.writeValueAsString(org);
System.out.println(jsonString);
}
catch (Exception e) {
e.printStackTrace();
}
}
// Method to populate object data
public static Organisation getObjectData() {
Organisation org = new Organisation();
org.setOrganisation_name("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:

Key Points:
- ObjectMapper is the core class of Jackson for JSON processing
- writeValueAsString() converts a Java object into a JSON string
- Jackson automatically maps Java fields to JSON keys using getters
- Exception handling is required as serialization may throw checked exceptions