Convert String to Java Object, Array and List with Jackson
Jackson is a widely-used library in the Java ecosystem for working with JSON data. The ObjectMapper class, a core component of Jackson, simplifies the process of converting JSON string to Java objects, object arrays and list of objects.
Let’s begin by introducing the Person class, which we’ll use for our examples.
public class Person {
private Long id;
private String name;
private String dateOfBirth;
private List<Person> friends;
// Getters and setters (not shown for brevity)
}
JSON String to Java Object
The ObjectMapper class allows us to deserialize JSON string into Java objects. Consider the following JSON string:
{
"id": 1,
"name": "Alice",
"dateOfBirth": "1990-05-15",
"friends": [{"id":2,"name":"Bob"}]
}
We can convert this JSON string into a Person object as follows:
private static void jsonStringToObject() {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"id\":1,\"name\":\"Alice\",\"dateOfBirth\":\"1990-05-15\",\"friends\":[{\"id\":2,\"name\":\"Bob\"}]}";
Person person = null;
try {
person = objectMapper.readValue(json, Person.class);
System.out.println("Deserialized person object: " + person.getName());
System.out.println("Deserialized friend's name: " + person.getFriends().get(0).getName());
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
JSON String to Java Array
Suppose we have a JSON array of Person objects:
[
{
"id": 1,
"name": "Alice",
"dateOfBirth": "1990-05-15",
"friends": [
{
"id": 2,
"name": "Bob",
"dateOfBirth": "1985-08-20"
}
]
},
{
"id": 2,
"name": "Bob",
"dateOfBirth": "1985-08-20",
"friends": [
{
"id": 1,
"name": "Alice",
"dateOfBirth": "1990-05-15"
}
]
}
]
We can convert this JSON array into a Java array of Person objects:
private static void jsonStringToArray() {
String jsonArray = "[{\"id\":1,\"name\":\"Alice\",\"dateOfBirth\":\"1990-05-15\",\"friends\":[{\"id\":2,\"name\":\"Bob\"}]},"
+ "{\"id\":2,\"name\":\"Bob\",\"dateOfBirth\":\"1985-08-20\",\"friends\":[{\"id\":1,\"name\":\"Alice\"}]}]";
ObjectMapper objectMapper = new ObjectMapper();
Person[] people;
try {
people = objectMapper.readValue(jsonArray, Person[].class);
for (Person person : people) {
System.out.println("Deserialized Person: " + person.getName());
if (!person.getFriends().isEmpty()) {
System.out.println("Deserialized Friend's name: " + person.getFriends().get(0).getName());
}
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
JSON String to Java List
If we have a JSON array of Person objects and want to convert it into a Java list:
private static void jsonStringToList() {
String jsonArray = "[{\"id\":1,\"name\":\"Alice\",\"dateOfBirth\":\"1990-05-15\",\"friends\":[{\"id\":2,\"name\":\"Bob\"}]},"
+ "{\"id\":2,\"name\":\"Bob\",\"dateOfBirth\":\"1985-08-20\",\"friends\":[{\"id\":1,\"name\":\"Alice\"}]}]";
ObjectMapper objectMapper = new ObjectMapper();
List<Person> people;
try {
people = objectMapper.readValue(jsonArray, new TypeReference<List<Person>>() {});
for (Person person : people) {
System.out.println("Deserialized Person: " + person.getName());
if (!person.getFriends().isEmpty()) {
System.out.println("Deserialized Friend's name: " + person.getFriends().get(0).getName());
}
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}