Java Jackson ObjectMapper readValue()

1. Overview

In this article, we will learn how to use the readValue() method of the ObjectMapper class. This method is responsible for deserializing a JSON string into its corresponding Java object representation.

Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples

2. Development Steps

1. Initialize a new Maven project.

2. Incorporate the necessary Jackson dependencies.

3. Design a Java class, representing a User entity.

4. Utilize the ObjectMapper class and its readValue() method to transform a JSON string back to a User object.

5. Present the resulting User object's attributes.

3. Create a Maven Project

There are different ways to create a simple Maven project:

Create a Simple Maven Project using the Command Line Interface

Create a Simple Maven Project using  Eclipse IDE

Create a Simple Maven Project using  IntelliJ IDEA

4. Maven Dependencies

Open the pom.xml file, and add the following Jackson data binding dependency:

Include the Jackson databind dependency in your Maven's `pom.xml`:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.0</version>
</dependency>

5. Code Program

// User.java
public class User {
    private int id;
    private String name;
    private String email;
    // Constructors, getters, setters, and other related methods...
}
// MainApp.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainApp {
    public static void main(String[] args) {
        String jsonString = "{\"id\":1,\"name\":\"John Doe\",\"email\":\"[email protected]\"}";
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            User user = objectMapper.readValue(jsonString, User.class);
            System.out.println("User ID: " + user.getId());
            System.out.println("User Name: " + user.getName());
            System.out.println("User Email: " + user.getEmail());
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Output:

User ID: 1
User Name: John Doe
User Email: [email protected]

Code Explanation:

In the provided code:

1. We defined a simple User class, equipped with attributes: id, name, and email.

2. Within the MainApp class, a JSON string representing a user is declared.

3. An instance of ObjectMapper is generated. This class is Jackson's central tool for converting between Java objects and JSON.

4. We call the readValue() method on the ObjectMapper instance, passing the JSON string and specifying that we desire it to be deserialized into a User object.

5. The attributes of the resultant User object are then displayed.

6. Conclusion

The readValue() method of Jackson's ObjectMapper class provides a robust mechanism for converting a JSON string into its relevant Java object. By mastering this method, Java developers can handle JSON deserialization tasks efficiently and maintain the integrity and structure of their data.

Check out all the Java Jackson JSON tutorials and examples: 50+ Java Jackson JSON Tutorials with Examples

Comments