Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Convert Java Object to Json String using Jackson API

  • Difficulty Level : Medium
  • Last Updated : 16 Oct, 2020

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

In this article, Java object is converted into the JSON using Jackson API:
The steps to do this are as follows: 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  • Add jar files of Jackson (in case of Maven project add Jackson dependencies in pom.xml file)


  • Below is the screenshot showing this step: 

  • 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
    public String toString()
        return "Organisation [organisation_name="
            + organisation_name
            + ", description="
            + description
            + ", Employees="
            + Employees + "]";
  • Below is the screenshot showing this step: 

  • 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 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
        catch (IOException e) {
    // Get the data to be inserted into the object
    public static Organisation getObjectData(Organisation org)
        // Insert the data
        org.setDescription("A computer Science portal for Geeks");
        // Return the object
        return org;
  • Execute the process.
  • The output in the JSON will be as below: 
  "organisation_name" : "GeeksforGeeks",
  "description" : "A computer Science portal for Geeks",
  "Employee" : "2000"
  • Below is the screenshot showing this output: 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!