In this short article, you'll learn how to convert a Java Object to a JsonNode Object by 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 @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.