Convert Java Object to Json String using GSON - GeeksforGeeks

04 Apr, 2019
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: It is an open-source Java library which is used to serialize and deserialize Java objects to JSON.
Jackson API
In this article, Java object is converted into the JSON using GSON:
The steps to do this are as follows:
Add jar files of Jackson (in case of Maven project add Gson dependencies in the pom.xml file)<dependency>       <groupId>com.google.code.gson</groupId>       <artifactId>gson</artifactId>       <version>2.6.2</version>   </dependency>Below is the screenshot showing this step:-Create a POJO (Plain Old Java Object) to be converted into JSONpackage GeeksforGeeks.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:-Create a Java class for converting the Organisation object into JSON.package GeeksforGeeks.Geeks;  import com.google.gson.Gson;  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);          System.out.println("Json represenatation"                           + " of Object organisation is ");        // In the below line        // we have created a New Gson Object        // and call it's toJson inbuid function        // and passes the object of organisation        System.out.println(new Gson().toJson(org));    }      /** Get the data to be inserted into the object **/    public static Organisation getObjectData(Organisation org)    {          /**insert the data**/        org.setOrganisation_name("GeeksforGeeks");        org.setDescription("A computer Science portal for Geeks");        org.setEmployees(2000);          /**Return Object**/        return org;    }}Below is the screenshot showing this step:-Execute the processOutput JsonOutput
{
  "organisation_name" : "GeeksforGeeks",
  "description" : "A computer Science portal for Geeks",
  "Employee" : "2000"
}
Add jar files of Jackson (in case of Maven project add Gson dependencies in the pom.xml file)<dependency>       <groupId>com.google.code.gson</groupId>       <artifactId>gson</artifactId>       <version>2.6.2</version>   </dependency>Below is the screenshot showing this step:-
<dependency>       <groupId>com.google.code.gson</groupId>       <artifactId>gson</artifactId>       <version>2.6.2</version>   </dependency>
Below is the screenshot showing this step:-
Create a POJO (Plain Old Java Object) to be converted into JSONpackage GeeksforGeeks.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:-
package GeeksforGeeks.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:-
Create a Java class for converting the Organisation object into JSON.package GeeksforGeeks.Geeks;  import com.google.gson.Gson;  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);          System.out.println("Json represenatation"                           + " of Object organisation is ");        // In the below line        // we have created a New Gson Object        // and call it's toJson inbuid function        // and passes the object of organisation        System.out.println(new Gson().toJson(org));    }      /** Get the data to be inserted into the object **/    public static Organisation getObjectData(Organisation org)    {          /**insert the data**/        org.setOrganisation_name("GeeksforGeeks");        org.setDescription("A computer Science portal for Geeks");        org.setEmployees(2000);          /**Return Object**/        return org;    }}Below is the screenshot showing this step:-
package GeeksforGeeks.Geeks;  import com.google.gson.Gson;  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);          System.out.println("Json represenatation"                           + " of Object organisation is ");        // In the below line        // we have created a New Gson Object        // and call it's toJson inbuid function        // and passes the object of organisation        System.out.println(new Gson().toJson(org));    }      /** Get the data to be inserted into the object **/    public static Organisation getObjectData(Organisation org)    {          /**insert the data**/        org.setOrganisation_name("GeeksforGeeks");        org.setDescription("A computer Science portal for Geeks");        org.setEmployees(2000);          /**Return Object**/        return org;    }}
Below is the screenshot showing this step:-
Execute the process
Output JsonOutput
{
  "organisation_name" : "GeeksforGeeks",
  "description" : "A computer Science portal for Geeks",
  "Employee" : "2000"
}
Output
{
  "organisation_name" : "GeeksforGeeks",
  "description" : "A computer Science portal for Geeks",
  "Employee" : "2000"
}
Below is the screenshot showing Output on Console:
Java-String-Programs
JSON
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
HashMap in Java with Examples
Interfaces in Java
Object Oriented Programming (OOPs) Concept in Java
ArrayList in Java
How to iterate any Map in Java
Initialize an ArrayList in Java
Singleton Class in Java
Overriding in Java
Collections in Java
Multithreading in Java