Jackson ObjectMapper writeValueAsString()

1. Overview

The Jackson library is a powerful tool in the Java ecosystem to work with JSON data. Among its numerous functionalities, the ObjectMapper class stands out as one of the primary classes developers interact with. In this article, we'll dive into the writeValueAsString() method of the ObjectMapper class, which converts a Java object into its JSON string representation.

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

2. Development Steps

1. Set up a new Maven project.

2. Add the necessary Jackson dependencies.

3. Define a Java class representative of a User entity.

4. Use the ObjectMapper class and its writeValueAsString() method to convert the User object to a JSON string.

5. Display the resulting JSON string.

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:

<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 relevant methods...
}
// MainApp.java
import com.fasterxml.jackson.databind.ObjectMapper;
public class MainApp {
    public static void main(String[] args) {
        User user = new User(1, "Ramesh Fadatare", "ramesh.fadatare@example.com");
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String jsonString = objectMapper.writeValueAsString(user);
            System.out.println(jsonString);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Output:

{"id":1,"name":"Ramesh Fadatare","email":"ramesh.fadatare@example.com"}

Code Explanation:

In the provided code:

1. We've declared a simple User class with some fields: id, name, and email.

2. In the MainApp class, we first instantiate a User object.

3. An instance of ObjectMapper is then created, which is Jackson's main mechanism for converting between Java objects and JSON.

4. The writeValueAsString() method is invoked on the ObjectMapper instance, passing in our User object. This method converts the User object into a JSON string.

5. The resultant JSON string is printed to the console.

6. Conclusion

The writeValueAsString() method of Jackson's ObjectMapper class offers a concise and straightforward way to convert Java objects into their JSON representation. By understanding and employing this method, Java developers can seamlessly handle JSON serialization tasks within their applications.

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

Comments