Convert Java Object to Json String using Jackson API
Last Updated :
10 May, 2022
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
// Java Program to Illustrate Organisation Class
package com.Geeks;
// Importing required classes
public class Organisation {
// Class data members
private String organisation_name;
private String description;
private int Employees;
// Calling getters and setters
// Getter
public String getOrganisation_name()
{
return organisation_name;
}
// Setter
public void
setOrganisation_name(String organisation_name)
{
this.organisation_name = organisation_name;
}
// Getter
public String getDescription() { return description; }
// Setter
public void setDescription(String description)
{
this.description = description;
}
// Getter
public int getEmployees() { return Employees; }
// Setter
public void setEmployees(int employees)
{
Employees = employees;
}
// Method
// Creating toString
@Override public String toString()
{
// Returning attributes of organisation
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
// Java Program to Illustrate Object to JSON Conversion
package com.Geeks;
// Importing required classes
import com.Geeks.Organisation;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
// Class
public class ObjectToJson {
// Main driver method
public static void main(String[] a)
{
// Creating object of Organisation
Organisation org = new Organisation();
// Insert the data into the object
org = getObjectData(org);
// Creating Object of ObjectMapper define in Jackson
// Api
ObjectMapper Obj = new ObjectMapper();
// Try block to check for exceptions
try {
// Getting organisation object as a json string
String jsonStr = Obj.writeValueAsString(org);
// Displaying JSON String on console
System.out.println(jsonStr);
}
// Catch block to handle exceptions
catch (IOException e) {
// Display exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
// Method
// Getting the data to be inserted
// into the object
public static Organisation
getObjectData(Organisation org)
{
// Insert the custom data
org.setName("GeeksforGeeks");
org.setDescription(
"A computer Science portal for Geeks");
org.setEmployees(2000);
// Returning the object
return org;
}
Step 3: Execute the process.
The output in the JSON will be as below:
Output:

Similar Reads
Convert Json String to Java Object Using GSON Pre-requisite: Convert Java Object to Json String Using GSONJSON Stand for JavaScript Object Notation. It's a standard text-based format which shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmi
3 min read
How to Convert JS Object to JSON String in JQuery/Javascript? Converting a JavaScript object to a JSON string means using the JSON.stringify() method to transform the object into a JSON-formatted string. This allows for efficient data storage, transmission, and debugging by representing complex data structures in a standardized text format.To Convert JS Object
4 min read
How to parse JSON object using JSON.stringify() in JavaScript ? In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax:
2 min read
How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used
3 min read
JSON using Jackson in REST API Implementation with Spring Boot When we build REST APIs with Spring Boot, we need to exclude NULL values from the JSON responses. This is useful when we want to optimize the data being transferred, making the response more compact and easier to process for the client.In this article, we are going to learn the approach that is used
4 min read
Spring Boot - Consume JSON Object From Kafka Topics Apache Kafka is a publish-subscribe messaging system. A messaging system lets someone is sending messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Applications may connect
4 min read