In this brief tutorial, you'll learn how to use Jackson to convert a Java Object to its JSON representation and vice versa. Jackson provides writeValue() and readValue() methods to convert Java Objects to and from JSON.

Dependencies

To add Jackson to your Gradle project, add the following dependency to the build.gradle file:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.0'

For Maven, include the below dependency to your pom.xml file:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.10.0</version>
</dependency>

Create Java Class

Let us create a simple Java class for testing purposes:

User.java

public class User {

    public String name;
    public String email;
    private String[] roles;
    private boolean admin;

    public User() {
    }

    public User(String name, String email, String[] roles, boolean admin) {
        this.name = name;
        this.email = email;
        this.roles = roles;
        this.admin = admin;
    }

    // getters and setters, toString() .... (omitted for brevity)
}

Convert Java Object to JSON String

Here is an example that uses the writeValueAsString() method from ObjectMapper to transform a Java Object to its JSON representation:

try {
    // create user object
    User user = new User("John Doe", "john.doe@example.com",
            new String[]{"Member", "Admin"}, true);

    // convert user object to JSON
    String json = new ObjectMapper().writeValueAsString(user);

    // print JSON string
    System.out.println(json);

} catch (Exception ex) {
    ex.printStackTrace();
}

The JSON string generated by the above code snippet:

{"name":"John Doe","email":"john.doe@example.com","roles":["Member","Admin"],"admin":true}

Convert Java Object to JSON File

The following example shows how to use the writeValue() method from ObjectMapper to write a Java Object to a JSON file:

try {
    // create user object
    User user = new User("John Doe", "john.doe@example.com",
            new String[]{"Member", "Admin"}, true);

    // convert user object to JSON file
    new ObjectMapper().writeValue(Paths.get("user.json").toFile(), user);

} catch (Exception ex) {
    ex.printStackTrace();
}

Convert JSON String to Java Object

To convert a JSON string back to Java Object, we can use the readValue() method from the ObjectMapper class:

try {
    // user as JSON string
    String json = "{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
            "\"roles\":[\"Member\",\"Admin\"],\"admin\":true}";

    // convert JSON string to Java Object
    User user = new ObjectMapper().readValue(json, User.class);

    // print user
    System.out.println(user);

} catch (Exception ex) {
    ex.printStackTrace();
}

The above example outputs the following on the console:

User{name='John Doe', email='john.doe@example.com', roles=[Member, Admin], admin=true}

Convert JSON File to Java Object

The readValue() method also accepts an instance of File to parse its content into a Java Object. Here is an example:

try {
    // convert JSON file to Java Object
    User user = new ObjectMapper().readValue(Paths.get("user.json").toFile(), User.class);

    // print user
    System.out.println(user);

} catch (Exception ex) {
    ex.printStackTrace();
}

For more Jackson examples, check out the How to read and write JSON using Jackson in Java tutorial.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.