Convert a JSON array to a list with Java Jackson

In this lesson, you will learn how to convert a JSON array to a Java list. We will use the ObjectMapper class from the Jackson library.

Let’s say we have a JSON that is an array of objects:

[
   {
      "name":"John",
      "grade":8,
      "city":"Boston"
   },
   {
      "name":"Tom",
      "grade":9,
      "city":"Denver"
   },
   {
      "name":"Megan",
      "grade":8,
      "city":"New York city"
   }
]


To convert the above JSON array to a list of objects, we need a Java class (POJO). Let’s create a class with the name Student:

class Student {

  private String name;
  private int grade;
  private String city;

 // constructors, getters, setters and toString() method...
}


Note
: We need the default constructor, getters, and setters methods for the Jackson conversion. Jackson maps the fields by matching the names of the JSON fields to the getter and setter methods in the Java object.

Now, let’s convert the JSON array to a list.  Since List is generic, we need to use the TypeReference class.

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

class Test {

  public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    String jsonArray = "[{\"name\":\"John\",\"grade\":8,\"city\":\"Boston\"}," +
            "{\"name\":\"Tom\",\"grade\":9,\"city\":\"Denver\"}," +
            "{\"name\":\"Megan\",\"grade\":8,\"city\":\"New York city\"}]";

    List<Student> studentList = objectMapper.readValue(jsonArray, new TypeReference<List<Student>>() {});

    System.out.println(studentList);
  }
}
Output: [Student{name=’John’, grade=0, city=’Boston’}, Student{name=’Tom’, grade=0, city=’Denver’}, Student{name=’Megan’, grade=0, city=’New York city’}]
 
We used the readValue() method from the ObjectMapper class that accepts a String as the first argument and a TypeRef as the second. If you need to read the JSON array from a file, you first need to convert it to a String and then call the readValue() method.
 
See the below example:
 
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;

class Test {

  public static void main(String[] args) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    File file = new File("path_to_file/student.json");

    JsonNode jsonArray = objectMapper.readValue(file, JsonNode.class);

    String jsonArrayAsString = objectMapper.writeValueAsString(jsonArray);

    List<Student> studentList = objectMapper.readValue(jsonArrayAsString, new TypeReference<List<Student>>() {});

    System.out.println(studentList);
  }
}
Output: [Student{name=’John’, grade=0, city=’Boston’}, Student{name=’Tom’, grade=0, city=’Denver’}, Student{name=’Megan’, grade=0, city=’New York city’}]
 
That’s it!
 
Proceed to the next lesson to learn how to convert a JSON to Java Map. 
 
Happy coding!

Leave a Reply

Your email address will not be published.