# Convert Java Object to JsonNode using Jackson

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 17, 2019, updated Oct 14, 2022. Topics: Java, JSON, Jackson.
Canonical URL: https://attacomsian.com/blog/jackson-convert-java-object-to-json-node

> A short tutorial to learn how to convert a Java Object or a Map to a JsonNode Object using Jackson.

In this short article, you'll learn how to convert a Java Object to a `JsonNode` Object using the [Jackson](https://attacomsian.com/blog/jackson-read-write-json) library.

## Convert Java Object to `JsonNode`

Let us say we have the following class named `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)
}
```

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

```java
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:

```java
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:

```java
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`:

```java
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](https://attacomsian.com/blog/jackson-json-node-tree-model) for more `JsonNode` examples.

For more Jackson examples, check out the [How to read and write JSON using Jackson in Java](https://attacomsian.com/blog/jackson-read-write-json) tutorial.
