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'

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.

Convert json string array to java list
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().
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(); 
}

Hope the article was useful.

0
Liked the article ? Spread the word...