In this short tutorial, you'll learn how to use the Jackson library to convert a JSON array string into a list of Java Objects and vice versa.
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>
Example JSON Array
Here is a JSON array that we want to convert into a list of Java Objects:
[
{
"name": "John Doe",
"email": "john.doe@example.com",
"roles": [
"Member",
"Admin"
],
"admin": true
},
{
"name": "Tom Lee",
"email": "tom.lee@example.com",
"roles": [
"Member"
],
"admin": false
}
]
Convert JSON Array to Java List
To convert the above JSON array to a list of Java Objects, let us first create a simple User
class to map JSON fields:
public class User {
public String name;
public String email;
private String[] roles;
private boolean admin;
public User() {
}
public User(String name, String email, String[] roles, boolean admin) {
this.name = name;
this.email = email;
this.roles = roles;
this.admin = admin;
}
// getters and setters, toString() .... (omitted for brevity)
}
Now we can use the readValue()
method from the ObjectMapper
class to transform the above JSON array to a list of User
objects, as shown below:
try {
// JSON array
String json = "[{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"," +
"\"roles\":[\"Member\",\"Admin\"],\"admin\":true},{\"name\":\"Tom Lee\"," +
"\"email\":\"tom.lee@example.com\",\"roles\":[\"Member\"],\"admin\":false}]";
// convert JSON array to Java List
List<User> users = new ObjectMapper().readValue(json, new TypeReference<List<User>>() {});
// print list of users
users.forEach(System.out::println);
} catch (Exception ex) {
ex.printStackTrace();
}
You should see the following output printed on the console:
User{name='John Doe', email='john.doe@example.com', roles=[Member, Admin], admin=true}
User{name='Tom Lee', email='tom.lee@example.com', roles=[Member], admin=false}
To convert the JSON array into an equivalent Java array, you should do the following:
User[] users = new ObjectMapper().readValue(json, User[].class);
If your JSON array is stored in a JSON file, you can still read and parse its content to a list of Java Objects, as shown below:
List<User> users = new ObjectMapper().readValue(Paths.get("users.json").toFile(),
new TypeReference<List<User>>() {
});
Convert List of Java Objects to JSON Array
The following example demonstrates how to use the writeValueAsString()
method from the ObjectMapper
class to convert a list of Java Objects to their JSON array representation:
try {
// create a list of users
List<User> users = Arrays.asList(
new User("John Doe", "john.doe@example.com",
new String[]{"Member", "Admin"}, true),
new User("Tom Lee", "tom.lee@example.com",
new String[]{"Member"}, false)
);
// convert users list to JSON array
String json = new ObjectMapper().writeValueAsString(users);
// print JSON string
System.out.println(json);
} catch (Exception ex) {
ex.printStackTrace();
}
You should see the following output:
[{"name":"John Doe","email":"john.doe@example.com","roles":["Member","Admin"],"admin":true},
{"name":"Tom Lee","email":"tom.lee@example.com","roles":["Member"],"admin":false}]
To write a list of Java Objects directly to a JSON file, we can use the writeValue()
method:
try {
// create a list of users
List<User> users = Arrays.asList(
new User("John Doe", "john.doe@example.com",
new String[]{"Member", "Admin"}, true),
new User("Tom Lee", "tom.lee@example.com",
new String[]{"Member"}, false)
);
// convert users list to JSON file
new ObjectMapper().writeValue(Paths.get("users.json").toFile(), users);
} catch (Exception ex) {
ex.printStackTrace();
}
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.