# How to write JSON to a file using Jackson

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Nov 27, 2019, updated Oct 14, 2022. Topics: Java, JSON, Jackson.
Canonical URL: https://attacomsian.com/blog/jackson-write-json-file

> A quick tutorial to learn how to write  JSON to a file using Jackson API. 

In this quick tutorial, you'll **learn how to write JSON data to a file** by using the [Jackson API](https://attacomsian.com/blog/jackson-read-write-json). Jackson is a popular JSON processing library for reading, writing, and parsing JSON data in Java.

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

## Writing Java map to a JSON file

To write a Java `Map` to a JSON file, you can use the `writeValue()` method from `ObjectMapper` as shown below:

```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 JSON file
    mapper.writeValue(Paths.get("user.json").toFile(), map);

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

The above code will produce the following JSON file:

<p class="file-desc"><span>user.json</span></p>

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

## Writing Java Object to a JSON file

Let us first create a simple Java class named `Book.java` that we will use to convert a Java Object to a JSON file:

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

```java
public class Book {

    private String title;
    private String isbn;
    private long year;
    private String[] authors;

    public Book() {
    }

    public Book(String title, String isbn, long year, String[] authors) {
        this.title = title;
        this.isbn = isbn;
        this.year = year;
        this.authors = authors;
    }

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

The following example shows how you can write an instance of the `Book` class to a JSON file:

```java
try {
    // create book object
    Book book = new Book("Thinking in Java", "978-0131872486", 1998,
            new String[]{"Bruce Eckel"});

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

    // convert book object to JSON file
    mapper.writeValue(Paths.get("book.json").toFile(), book);

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

The above code will produce the following `book.json` file:

```json
{"title":"Thinking in Java","isbn":"978-0131872486","year":1998,"authors":["Bruce Eckel"]}
```

## Writing a list of Java objects to a JSON file

Just like a single Java Object, you can also write a list of Java Objects to a JSON file using the same `writeValue()` method:

```java
try {
    // create a books list
    List<Book> books = Arrays.asList(
            new Book("Thinking in Java", "978-0131872486", 1998,
                    new String[]{"Bruce Eckel"}),
            new Book("Head First Java", "0596009208", 2003,
                    new String[]{"Kathy Sierra", "Bert Bates"})
    );

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

    // convert book object to JSON file
    mapper.writeValue(Paths.get("books.json").toFile(), books);

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

The above code will produce the following `books.json` file:

```java
[{"title":"Thinking in Java","isbn":"978-0131872486","year":1998,"authors":["Bruce Eckel"]},
  {"title":"Head First Java","isbn":"0596009208","year":2003,"authors":["Kathy Sierra","Bert Bates"]}]
```

## Write Java Object to a pretty print JSON file

You can use the `DefaultPrettyPrinter` class to enable the[ pretty-print JSON](https://attacomsian.com/blog/jackson-pretty-print-json) output:

```java
try {
    // create book object
    Book book = new Book("Thinking in Java", "978-0131872486", 1998,
            new String[]{"Bruce Eckel"});

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

    // create an instance of DefaultPrettyPrinter
    ObjectWriter writer = mapper.writer(new DefaultPrettyPrinter());

    // convert book object to JSON file
    writer.writeValue(Paths.get("book.json").toFile(), book);

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

Here is how the `book.json` file looks like after JSON pretty-print is enabled:

```json
{
  "title" : "Thinking in Java",
  "isbn" : "978-0131872486",
  "year" : 1998,
  "authors" : [ "Bruce Eckel" ]
}
```

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.
