JSON
Java
Data Conversion
Programming
Object-Oriented Programming

Converting JSON data to Java object

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. In Java development, converting JSON data into Java objects is a common task, particularly in web and mobile applications that consume APIs. This conversion process is known as JSON deserialization. This article discusses several ways to perform this JSON to Java object conversion, focusing primarily on using popular libraries like Jackson and Gson, as well as considering built-in options provided by Java.

Why Convert JSON to Java Objects?

Conversion of JSON to Java objects (and vice versa) is fundamental to modern web services and APIs, which often exchange data in JSON format. Having JSON deserialized into Java objects allows developers to work within the strong type system of Java, thus making code easier to write, understand, and debug by providing compile-time type-checking and auto-completion features in IDEs.

Using Jackson

Jackson is a high-performance JSON processor for Java. It supports JSON to Java object conversion out of the box through its powerful data binding capabilities. To use Jackson, you need to add the Jackson library to your project:

xml
1<!-- Add this to your pom.xml if you are using Maven -->
2<dependency>
3    <groupId>com.fasterxml.jackson.core</groupId>
4    <artifactId>jackson-databind</artifactId>
5    <version>2.12.3</version>
6</dependency>

Here's an example of how you can deserialize a JSON string to a Java object using Jackson:

java
1import com.fasterxml.jackson.databind.ObjectMapper;
2
3public class Main {
4    public static void main(String[] args) throws IOException {
5        String json = "{\"name\":\"John\", \"age\":30}";
6        ObjectMapper mapper = new ObjectMapper();
7        Person person = mapper.readValue(json, Person.class);
8        System.out.println(person.getName());
9    }
10}
11
12class Person {
13    private String name;
14    private int age;
15    // Getters and setters omitted for brevity
16}

In this example, ObjectMapper reads the JSON data and creates a Person object based on the keys in the JSON string.

Using Gson

Another popular library is Gson, provided by Google. Gson can be used for converting JSON strings into Java objects very easily. To use Gson in your project, include the following dependency:

xml
1<!-- Add this to your pom.xml if you are using Maven -->
2<dependency>
3    <groupId>com.google.code.gson</groupId>
4    <artifactId>gson</artifactId>
5    <version>2.8.6</version>
6</dependency>

Below is an example of deserializing JSON using Gson:

java
1import com.google.gson.Gson;
2
3public class Main {
4    public static void main(String[] args) {
5        String json = "{\"name\":\"John\", \"age\":30}";
6        Gson gson = new Gson();
7        Person person = gson.fromJson(json, Person.class);
8        System.out.println(person.getName());
9    }
10}
11
12class Person {
13    private String name;
14    private int age;
15    // Getters and setters omitted for brevity
16}

Gson's fromJson method directly maps the JSON data to the Java object.

Java Built-in JSON Support

With the introduction of JSON-P and JSON-B in Java EE, Java now includes built-in support for JSON processing. JSON-B (JSON Binding) allows conversion between JSON texts and Java objects. To utilize it, you might need to add a dependency on the JSON-B API and a compatible implementation, such as Yasson.

java
1import javax.json.bind.Jsonb;
2import javax.json.bind.JsonbBuilder;
3
4public class Main {
5    public static void main(String[] args) {
6        Jsonb jsonb = JsonbBuilder.create();
7        String json = "{\"name\":\"John\", \"age\":30}";
8        Person person = jsonb.fromJson(json, Person.class);
9        System.out.println(person.getName());
10    }
11}

Comparison Table

FeatureJacksonGsonJava Built-in
PerformanceHighHighModerate
PopularityHighHighModerate
CustomizationExtensiveGoodLimited
Non-Standard JSONGood SupportGood SupportVaries
Community/SupportVery ActiveActiveVaries

Conclusion

There are various tools at a Java developer's disposal for JSON deserialization, each with its strengths and ideal use cases. Libraries like Jackson and Gson offer extensive customization, performance, and ease of use, making them popular choices in many projects. Meanwhile, Java's built-in support is continually improving and might suit simpler applications or those that strictly adhere to newer Java standards.


Course illustration
Course illustration

All Rights Reserved.