Convert Java Object to Json String using GSON
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>chevron_rightfilter_noneBelow is the screenshot showing this step:-
- Create a POJO (Plain Old Java Object) to be converted into JSON
packageGeeksforGeeks.Geeks;publicclassOrganisation {privateString organisation_name;privateString description;privateintEmployees;// Calling getters and setterspublicString getOrganisation_name(){returnorganisation_name;}publicvoidsetOrganisation_name(String organisation_name){this.organisation_name = organisation_name;}publicString getDescription(){returndescription;}publicvoidsetDescription(String description){this.description = description;}publicintgetEmployees(){returnEmployees;}publicvoidsetEmployees(intemployees){Employees = employees;}// Creating toString@OverridepublicString toString(){return"Organisation [organisation_name="+ organisation_name+", description="+ description+", Employees="+ Employees +"]";}}chevron_rightfilter_noneBelow is the screenshot showing this step:-

- Create a Java class for converting the Organisation object into JSON.
packageGeeksforGeeks.Geeks;importcom.google.gson.Gson;publicclassObjectToJson {publicstaticvoidmain(String[] a){/**Creating object of Organisation **/Organisation org =newOrganisation();/** 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 organisationSystem.out.println(newGson().toJson(org));}/** Get the data to be inserted into the object **/publicstaticOrganisation getObjectData(Organisation org){/**insert the data**/org.setOrganisation_name("GeeksforGeeks");org.setDescription("A computer Science portal for Geeks");org.setEmployees(2000);/**Return Object**/returnorg;}}chevron_rightfilter_noneBelow is the screenshot showing this step:-

- Execute the process
- Output Json
Output { "organisation_name" : "GeeksforGeeks", "description" : "A computer Science portal for Geeks", "Employee" : "2000" }
Below is the screenshot showing Output on Console:

Recommended Posts:
- Convert Json String to Java Object Using GSON
- Convert Java Object to Json String using Jackson API
- Python | Ways to convert string to json object
- JSON | modify an array value of a JSON object
- JavaScript | How to add an element to a JSON object?
- Deserializing a JSON into a JavaScript object
- JavaScript | Add new attribute to JSON object
- How to convert PHP array to JavaScript or JSON ?
- JavaScript | Convert an array to JSON
- Converting JSON text to JavaScript Object
- JavaScript | Check if a key exists inside a JSON object
- Convert a List of String to a comma separated String in Java
- Convert an ArrayList of String to a String array in Java
- Convert a Set of String to a comma separated String in Java
- String Literal Vs String Object in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



