# How to Read JSON from 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-read-json-file

> A quick tutorial to learn how to read JSON data from a file using Jackson API.

In this quick tutorial, you'll **learn how to read JSON data from 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>
```

## Read JSON File to a Java Map

Let us say you have the following JSON file called `book.json`:

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

To read the above JSON file contents to a Java `Map`, you can use the `readValue()` method from `ObjectMapper` as shown below:

```java
try {
    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON file to map
    Map<?, ?> map = mapper.readValue(Paths.get("book.json").toFile(), Map.class);

    // print map entries
    for (Map.Entry<?, ?> entry : map.entrySet()) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }

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

You should see the following output printed on the console:

```bash
title=Thinking in Java
isbn=978-0131872486
year=1998
authors=[Bruce Eckel]
```

## Read JSON File to a Java Object

Let us first create a simple Java class called `Book.java` to map the JSON object:

<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 demonstrates how you can read the above JSON file into a `Book` object by using the `readValue()` method:

```java
try {
    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert a JSON string to a Book object
    Book book = mapper.readValue(Paths.get("book.json").toFile(), Book.class);

    // print book
    System.out.println(book);

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

The above code will output the following on the console:

```bash
Book{title='Thinking in Java', isbn='978-0131872486', year=1998, authors=[Bruce Eckel]}
```

## Read JSON File to a List of Java Objects

Suppose we have the following JSON file called `books.json` that contains a JSON array:

```json
[
  {
    "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"
    ]
  }
]
```

You can now read a list of `Book` objects from the above JSON file using the same `readValue()` method as shown below:

```java
try {
    // create object mapper instance
    ObjectMapper mapper = new ObjectMapper();

    // convert JSON array to list of books
    List<Book> books = Arrays.asList(mapper.readValue(Paths.get("books.json").toFile(), Book[].class));

    // print books
    books.forEach(System.out::println);

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

Here is the output of the above code:

```bash
Book{title='Thinking in Java', isbn='978-0131872486', year=1998, authors=[Bruce Eckel]}
Book{title='Head First Java', isbn='0596009208', year=2003, authors=[Kathy Sierra, Bert Bates]}
```

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.
