How to Read JSON from a file using Jackson

In this quick tutorial, you'll learn how to read JSON data from a file by using the Jackson API. Jackson is a popular JSON processing library for reading, writing, and parsing JSON data in Java.

Dependencies

To add Jackson to your Gradle project, add the following dependency to the build.gradle file:

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

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

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

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

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:

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:

Book.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:

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:

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:

[
  {
    "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:

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:

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 tutorial.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.