Convert Java Object to JsonNode using Jackson

In this short article, you'll learn how to convert a Java Object to a JsonNode Object using the Jackson library.

Convert Java Object to JsonNode

Let us say we have the following class named User:

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)
}

To convert the above User class instance to a JsonNode object, you can use the valueToTree() method from ObjectMapper, as shown below:

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

    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert user object to `JsonNode`
    JsonNode node = mapper.valueToTree(user);

    // print JSON nodes
    System.out.println(node.path("name").asText());
    System.out.println(node.path("email").asText());
    System.out.println(node.path("roles").get(0).asText());

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

Alternatively, you can also use the convertValue() method from the ObjectMapper class:

JsonNode node = mapper.convertValue(user, JsonNode.class);

Convert Map to JsonNode

The following example demonstrates how to convert a Java Map object to a JsonNode object using the same convertValue() method:

try {
    // create a map
    Map<String, Object> map = new HashMap<>();
    map.put("name", "John Deo");
    map.put("email", "john.doe@example.com");
    map.put("roles", new String[]{"Member", "Admin"});
    map.put("admin", true);

    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert map to `JsonNode`
    JsonNode node = mapper.convertValue(map, JsonNode.class);

    // print JSON nodes
    System.out.println(node.path("name").asText());
    System.out.println(node.path("email").asText());
    System.out.println(node.path("roles").get(0).asText());

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

Convert JsonNode to Java Object

To convert an instance of JsonNode to a Java Object, you can use the treeToValue() method from ObjectMapper:

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

    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON string to `JsonNode`
    JsonNode node = mapper.readTree(json);

    // convert `JsonNode` to `User` object
    User user = mapper.treeToValue(node, User.class);

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

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

Read the guide Working with Tree Model Nodes in Jackson for more JsonNode examples.

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.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.