Convert Json String to Java Object Using GSON Last Updated : 24 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Pre-requisite: Convert Java Object to Json String Using GSONJSON 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, a predefined JSON String is converted into Java Object using GSON.Examples: Input: { "organisation_name" : "GeeksforGeeks", "description" : "A computer Science portal for Geeks", "Employee" : "2000" } Output: Organisation [organisation_name=GeeksforGeeks, description=A computer Science portal for Geeks, Employees=0]Input: { "Student_name" : "XYZ", "Organisation_name" : "GeeksForGeeks" "Roll_No" : "1" } Output: Student [Student_name=XYZ, Organisation_name=GeeksForGeeks, Roll_no=1] 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) html <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 JSON Java 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 String Variable for Storing Json String: Note: This Json string should not be a simple Json String. Preprocess the JSON String and add slashes before passing it into GSON object.Example of Preprocessing: Initial JSON String: {"organisation_name" : "GeeksforGeeks", "description" : "A computer Science portal for Geeks", "Employee" : "2000"}Preprocessed JSON String: { \"organisation_name\" : \"GeeksforGeeks\", \"description\" : \"A computer Science portal for Geeks\", \"Employee\" : \"2000\" } Create a Java class for converting the Json into Organisation object: Java package GeeksforGeeks.Geeks; import com.google.gson.Gson; public class JsonToObject { public static void main(String[] args) { // Creating object of Organisation Organisation org = new Organisation(); // Converting json to object org = getOrganisationObject(); // Displaying object System.out.println(org); } private static Organisation getOrganisationObject() { // Storing preprocessed json(Added slashes) String OrganisationJson = "{\"organisation_name\" : \"GeeksforGeeks\", \"description\" : \"A computer Science portal for Geeks\", \"Employee\" : \"2000\"}"; // Creating a Gson Object Gson gson = new Gson(); // Converting json to object // first parameter should be preprocessed json // and second should be mapping class Organisation organisation = gson.fromJson(OrganisationJson, Organisation.class); // return object return organisation; } } Below is the screenshot showing this step:- Execute the process Output: Organisation [organisation_name=GeeksforGeeks, description=A computer Science portal for Geeks, Employees=0] Comment More infoAdvertise with us Next Article Convert Json String to Java Object Using GSON S Shahnawaz_Ali Follow Improve Article Tags : Java Java-Object Oriented JSON Practice Tags : Java Similar Reads Convert Java Object to Json String using Jackson API JSON stands for JavaScript Object Notation. It's a standard text-based format that 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. In order to con 3 min read How to Convert JS Object to JSON String in JQuery/Javascript? Converting a JavaScript object to a JSON string means using the JSON.stringify() method to transform the object into a JSON-formatted string. This allows for efficient data storage, transmission, and debugging by representing complex data structures in a standardized text format.To Convert JS Object 4 min read How to parse JSON object using JSON.stringify() in JavaScript ? In this article, we will see how to parse a JSON object using the JSON.stringify function. The JSON.stringify() function is used for parsing JSON objects or converting them to strings, in both JavaScript and jQuery. We only need to pass the object as an argument to JSON.stringify() function. Syntax: 2 min read How to Convert JSON to string in JavaScript ? In this article, we are going to learn the conversion of JSON to string in JavaScript. Converting JSON to a string in JavaScript means serializing a JavaScript object or data structure represented in JSON format into a textual JSON string for data storage or transmission.Several methods can be used 3 min read How to Convert String to JSON in JavaScript? In JavaScript, converting a string to JSON is important for handling data interchangeably between server and client, parsing external API responses, and storing structured data in applications. Below are the approaches to converting string to JSON in JavaScript: Table of Content Using JSON.parse()Us 2 min read Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are 4 min read Spring Boot - Consume JSON Object From Kafka Topics Apache Kafka is a publish-subscribe messaging system. A messaging system lets someone is sending messages between processes, applications, and servers. Broadly Speaking, Apache Kafka is software where topics (A topic might be a category) can be defined and further processed. Applications may connect 4 min read How to Convert Map to JSON in JavaScript ? In JavaScript, when working with data, you might encounter situations where you need to convert a Map object into a JSON format. This can be useful for sending data over the network, storing data in local storage, or interfacing with APIs that expect JSON data. Converting a Map to JSON means convert 3 min read How to Convert XML to JSON in JavaScript? To convert XML to JSON in JavaScript, various methods and libraries and be used. Here, we use xml-js library that provides xml2json function to convert XML to JSON data. It takes XML data as input and gives the JSON objects as output. We can also use the DOMParser from the xmldom package to convert 2 min read How to Convert Local Time to GMT in Java? Time conversion from IST or any standard time to GMT is necessary for locals to understand and reciprocate their international clients if they are connected overseas in terms of work or any purpose. Today we will have a look at a code where we convert the standard time of any country to GMT. Here we 4 min read Like