Convert Java Object to Json String using Jackson API
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:
- 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>chevron_rightfilter_noneBelow is the screenshot showing this step:

- Create a POJO (Plain Old Java Object) to be converted into JSON
Java Class
packagecom.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. Convert the object into JSON using ObjectMapper class of Jackson API.
packagecom.Geeks;importjava.io.IOException;importorg.codehaus.jackson.map.ObjectMapper;importcom.Geeks.Organisation;publicclassObjectToJson {publicstaticvoidmain(String[] a){// Creating object of OrganisationOrganisation org =newOrganisation();// Insert the data into the objectorg = getObjectData(org);// Creating Object of ObjectMapper define in Jakson ApiObjectMapper Obj =newObjectMapper();try{// get Oraganisation object as a json stringString jsonStr = Obj.writeValueAsString(org);// Displaying JSON StringSystem.out.println(jsonStr);}catch(IOException e) {e.printStackTrace();}}// Get the data to be inserted into the objectpublicstaticgetObjectData(Organisation org){// Insert the dataorg.setName("GeeksforGeeks");org.setDescription("A computer Science portal for Geeks");org.setEmployees(2000);// Return the objectreturnorg;}chevron_rightfilter_none - Execute the process.
- 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:
Recommended Posts:
- Convert Java Object to Json String using GSON
- Convert Json String to Java Object Using GSON
- Python | Ways to convert string to json object
- JSON | modify an array value of a JSON object
- JavaScript | Convert an array to JSON
- JavaScript | Add new attribute to JSON object
- Converting JSON text to JavaScript 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
- Convert Set of String to Array of String in Java
- String Literal Vs String Object in Java
- Convert String to Double in Java
- Convert String to Date in Java
- Convert List of Characters to String 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.



