
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Deserialize JSON into an Existing Object in Java
Deserialization is the process of converting a JSON string into a Java object. In this example, we will learn how to deserialize a JSON string into an existing object in Java.
There are libraries available in Java that can help us to deserialize a JSON string into an existing object. Some of the popular libraries are:
Let's learn how to use each of these libraries to deserialize a JSON string into an existing object in Java.
Deserializing JSON Object Using Gson
The Gson library is a Java library developed by Google, used to convert Java objects into their JSON representation and vice versa. It is commonly used in Java applications to handle JSON data.
To use the Gson library, we need to include it in our project. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency>
If you do not use Maven, you can download the jar file from here.
Steps
Following are the steps to deserialize a JSON into an existing object using Gson in Java:
- Import the Gson library.
- Create a class, like Person, with fields for name and age.
- In your main code, create a JSON string.
- Create a Gson object.
- Use the fromJson() method to convert the JSON string to a Person object.
- Print the Person object.
- Use the Gson object to deserialize the JSON string into the existing object.
- Print the updated object.
Example
In the below example, we will create a class Person with fields for name and age. We will create a JSON string and then deserialize it into an existing object using the Gson library.
Following is the code to deserialize a JSON into an existing object using Gson in Java:
import com.google.gson.Gson; public class GsonExample { public static void main(String[] args) { String json = "{"name":"Ansh","age":23}"; Gson gson = new Gson(); // existing object Person p1 = new Person("Aish", 22); // temp object Person temp = gson.fromJson(json, Person.class); // manually update fields p1.setName(temp.getName()); p1.setAge(temp.getAge()); // print updated System.out.println("Updated Name: " + p1.getName()); System.out.println("Updated Age: " + p1.getAge()); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } }
Output
Following is the output of the above code:
Updated Name: Ansh Updated Age: 23
Deserializing JSON Object Using Jackson
The Jackson library is also popular for handling JSON data in Java. It is known for its speed and efficiency. Jackson provides a simple way to convert Java objects to JSON and vice versa.
Similar to Gson, to use the Jackson library, we need to include it in our project. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.0</version> </dependency>
If you do not use Maven, you can download the jar file from here.
Steps
Following are the steps to deserialize a JSON into an existing object using Jackson in Java:
- Import the Jackson library.
- Create a class, like Person, with fields for name and age.
- Create a JSON string in your main code.
- Use an ObjectMapper to convert the JSON string to a Person object.
- Print the Person object.
- Deserialize the JSON string into an existing object using ObjectMapper.
- Print the updated object.
Example
In the below example, we will create a class Person with fields for name and age. We will create a JSON string and then deserialize it into an existing object using the Jackson library.
Following is the code to deserialize a JSON into an existing object using Jackson in Java:
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; public class JacksonExample { public static void main(String[] args) throws IOException { // Create a JSON string String jsonString = "{"name":"Ansh","age":23}"; // Create an ObjectMapper object ObjectMapper objectMapper = new ObjectMapper(); // Deserialize the JSON string into an existing object Person person = new Person("Aish", 22); JsonNode jsonNode = objectMapper.readTree(jsonString); ((ObjectNode) jsonNode).put("name", person.getName()); ((ObjectNode) jsonNode).put("age", person.getAge()); // Print the updated object System.out.println("Updated Name: " + person.getName()); System.out.println("Updated Age: " + person.getAge()); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Output
Following is the output of the above code:
Updated Name: Ansh Updated Age: 23
Deserializing JSON Object Using Flexjson
Flexjson is a lightweight Java library for serializing and deserializing Java beans, maps, arrays, and collections in JSON format. We can also deserialize a JSON string to an existing object using the deserializeInto() method of the JSONDeserializer class, this method deserializes the given input into the existing object target.
The values in the JSON input can overwrite values in the target object. This means if a value is included in JSON, a new object can be created and set into the existing object.
Now, let's learn how to use the Flexjson library. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>net.sf.flexjson</groupId> <artifactId>flexjson</artifactId> <version>3.3</version> </dependency>
If you do not use Maven, you can download the jar file from here and add it to your project.
Steps
Following are the steps to deserialize a JSON into an existing object using Flexjson in Java:
- Import the Flexjson library.
- Create a class, like Person, with fields for name and age.
- Create a JSON string in your main code.
- Use a JSONDeserializer to deserialize the JSON string into an existing object.
- Print the updated object.
- Use the deserializeInto() method to deserialize the JSON string into the existing object.
- Print the updated object.
Example
In the below example, we will create a class Person with fields for name and age. We will create a JSON string and then deserialize it into an existing object using the Flexjson library.
Following is the code to deserialize a JSON into an existing object using Flexjson in Java:
import net.sf.flexjson.JSONDeserializer; import net.sf.flexjson.JSONSerializer; import net.sf.flexjson.transformer.AbstractTransformer; import net.sf.flexjson.transformer.ObjectTransformer; public class FlexjsonExample { public static void main(String[] args) { // Create a JSON string String jsonString = "{"name":"Ansh","age":23}"; // Create a JSONDeserializer object JSONDeserializer<Person> deserializer = new JSONDeserializer<>(); // Deserialize the JSON string into an existing object Person person = new Person("Aish", 22); deserializer.deserializeInto(jsonString, person); // Print the updated object System.out.println("Updated Name: " + person.getName()); System.out.println("Updated Age: " + person.getAge()); } } class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
Output
Following is the output of the above code:
Updated Name: Ansh Updated Age: 23