In this quick tutorial, you'll learn how to write JSON data to 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>
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:
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:
user.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:
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 shows how you can write an instance of the Book
class to a JSON file:
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:
{"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:
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:
[{"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 output:
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:
{
"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 tutorial.
✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.