Convert json array to java list of objects

Suppose you have a json string that contains a json array as below

[
  {
  "brand":"Abc", 
  "model":"XYZ"
  },
  {
  "brand":"Def", 
  "model":"123"
  }
]

and it needs to be converted to an array or a list of java objects using Jackson library.
In this article, we will understand how to do this with Jackson.

Jackson Configuration
Before moving further, ensure that Jackson is added to the classpath using below dependency

Maven

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.13.0</version> 
</dependency>

Gradle

implementation 'com.fasterxml.jackson.core:jackson-core:2.13.0'

1. Convert json string array to java array
Below is a class that we will be using for json string to java object conversion using Jackson.

package com.codippa;

public class Laptop {
  private String brand;
  private String model;
  
  // getter and setter
}

Jackson ObjectMapper class can be used to convert json array string to a java array of objects as shown below

ObjectMapper mapper = new ObjectMapper(); 
String json = "[{\"brand\":\"Abc\", \"model\":\"XYZ\"}," + 
  "{\"brand\":\"Def\", \"model\":\"123\"}]"; 
try { 
  Laptop[] arr = mapper.readValue(json, Laptop[].class); 
  // iterate over array
  for(Laptop l : arr) {
    System.out.println("Brand:" + l.getBrand()); 
    System.out.println("Model:" + l.getModel()); 
    System.out.println("----------------");
  }
} catch (JsonMappingException e) { 
  e.printStackTrace(); 
} catch (JsonProcessingException e) { 
  e.printStackTrace(); 
}

readValue() method of ObjectMapper takes two arguments:

1. Json string which needs to be converted to java object.
2. Class type of java object.

Note the second argument to readValue() is of array type of class objects, since we need to convert it to an array.

2. Convert json array to java list – ObjectMapper
A json string which has multiple json objects can also be converted to a list of java objects.

For this, we need to pass an object of Jackson’s TypeReference class with its generic type as the type of object to which we want to convert.
In this case, this type will be java.util.List. Example,

ObjectMapper mapper = new ObjectMapper(); 
String json = "[{\"brand\":\"Abc\", \"model\":\"XYZ\"}," +
  "{\"brand\":\"Def\", \"model\":\"123\"}]"; 
try { 
  // convert to list
  List<Laptop> arr = mapper.readValue(json, 
     new TypeReference<List<Laptop>>(){}); 
  // iterate over array 
  for(Laptop l : arr) { 
    System.out.println("Brand:" + l.getBrand()); 
    System.out.println("Model:" + l.getModel());
    System.out.println("----------------"); 
  } 
} catch (JsonMappingException e) { 
  e.printStackTrace(); 
} catch (JsonProcessingException e) { 
  e.printStackTrace(); 
}

com.fasterxml.jackson.core.type.TypeReference is an abstract class. So, we need to pass its implementation to readValue().
3. Convert json string array to java list – Second method
Convert a json string to an array of objects as shown above and then convert this array to a list using Arrays.asList() method.
Example,

ObjectMapper mapper = new ObjectMapper(); 
String json = "[{\"brand\":\"Abc\", \"model\":\"XYZ\"}," 
+ "{\"brand\":\"Def\", \"model\":\"123\"}]"; 
try { 
  List<Laptop> arr = Arrays.asList(
                       mapper.readValue(json, Laptop[].class)); 
  // iterate over array 
  for(Laptop l : arr) { 
    System.out.println("Brand:" + l.getBrand()); 
    System.out.println("Model:" + l.getModel()); 
    System.out.println("----------------"); 
  } 
} catch (JsonMappingException e) { 
  e.printStackTrace(); 
} catch (JsonProcessingException e) { 
  e.printStackTrace(); 
}

4. Convert json array to java list – Third method
Below is another example to convert json array to a list in java with Jackson ObjectMapper.

ObjectMapper mapper = new ObjectMapper(); 
String json = "[{\"brand\":\"Abc\", \"model\":\"XYZ\"}," + 
               "{\"brand\":\"Def\", \"model\":\"123\"}]"; 
try { 
  List<Laptop> arr = mapper.readValue(json, 
                     TypeFactory.defaultInstance().
                     constructCollectionType(List.class, Laptop.class));
  // iterate over array 
  for(Laptop l : arr) { 
    System.out.println("Brand:" + l.getBrand()); 
    System.out.println("Model:" + l.getModel()); 
    System.out.println("----------------"); 
  } 
} catch (JsonMappingException e) { 
  e.printStackTrace(); 
} catch (JsonProcessingException e) { 
  e.printStackTrace(); 
}

This method uses an overloaded version of readValue(), which accepts a JavaType as second argument.
To create an object of JavaType, a static constructCollectionType() method is used, which defines the type of java collection object and its generic type.
Type of collection object is a list, while its generic type is the type of our object that will be contained in the list.

Hope the article was useful.