Convert Java Object to JSON String using Jackson API

Convert Java Object to JSON String using Jackson API

Learn how to Convert Java Object Into JSON String using Jackson API. JSON Stand for JavaScript Object Notation. JSON is highly recommended to transmit data between a server and web application.

JSON 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 transmit data between a server and web application.

To convert a Java object into JSON, the following methods can be used:

  • GSON
  • JACKSON API

In this article, Java object is converted into the JSON using Jackson API:

The steps to do this are as follows:

  1. Add jar files of Jackson (in case of Maven project add Jackson dependencies in pom.xml file)
<dependency> 
        <groupId>com.fasterxml.jackson.core</groupId> 
        <artifactId>jackson-databind</artifactId> 
        <version>2.5.3</version> 
</dependency> 

Below is the screenshot showing this step:

  1. Create a POJO (Plain Old Java Object) to be converted into JSON

Java Class

package com.Geeks; 

public class Organisation { 
    private String organisation_name; 
    private String description; 
    private int Employees; 

    // Calling getters and setters 
    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; 
    } 

    // Creating toString 
    @Override
    public String toString() 
    { 
        return "Organisation [organisation_name="
            + organisation_name 
            + ", description="
            + description 
            + ", Employees="
            + Employees + "]"; 
    } 
} 

Below is the screenshot showing this step:

  1. Create a Java class for converting the Organisation object into JSON. Convert the object into JSON using ObjectMapper class of Jackson API.
package com.Geeks; 

import java.io.IOException; 
import org.codehaus.jackson.map.ObjectMapper; 
import com.Geeks.Organisation; 

public class ObjectToJson { 
    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 Jakson Api 
        ObjectMapper Obj = new ObjectMapper(); 

        try { 

            // get Oraganisation object as a json string 
            String jsonStr = Obj.writeValueAsString(org); 

            // Displaying JSON String 
            System.out.println(jsonStr); 
        } 

        catch (IOException e) { 
            e.printStackTrace(); 
        } 
    } 

    // Get the data to be inserted into the object 
    public static getObjectData(Organisation org) 
    { 

        // Insert the data 
        org.setName("GeeksforGeeks"); 
        org.setDescription("A computer Science portal for Geeks"); 
        org.setEmployees(2000); 

        // Return the object 
        return org; 
    } 
  1. Execute the process.
  2. The output in the JSON will be as below:
Output
{
  "organisation_name" : "GeeksforGeeks",
  "description" : "A computer Science portal for Geeks",
  "Employee" : "2000"
}

Below is the screenshot showing this output:

json java web-development javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Why JAVA is better for web development ?

In the second decade of 21st century software IT industry got flooded with new technologies, there are so many options to choose from that it is next to impossible to choose the right one for development.