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 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 JSON string to 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 @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.