# Convert Java Object to JSON using Jackson

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

> A short tutorial to learn how to convert a Java Object to JSON string or file and vice versa by using the Jackson library.

In this brief tutorial, you'll learn how to use [Jackson](https://attacomsian.com/blog/jackson-read-write-json) 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](https://attacomsian.com/blog/install-gradle-on-ubuntu) project, add the following dependency to the `build.gradle` file:

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

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

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

<p class="file-desc"><span>User.java</span></p>

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

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

```json
{"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:

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

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

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

```java
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](https://attacomsian.com/blog/jackson-read-write-json) tutorial.
