Skip to content

Jackson parse as Array / List of objects

  • by
Jackson parse array / list of objects

1. Overview

In this article, we will learn to parse JSON array as an Array or List of Java objects using the Jackson 2.0 library.

To learn more about Jackson, refer to these articles.

2. Jackson parse as Array or List

We will use the following Student class with fields id and studentName as reference for our examples.

public class Student {
	
	public Student(long id, String name) {
		this.id = id;
		this.studentName = name;
	}
	
	public Student() {	}

	private long id;
	private String studentName;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", studentName=" + studentName + "]";
	}
}

2. Jackson parse as Array of Java objects

You can deserialize the JSON array as an Array of Java objects by using the readValue function:

public Class[] readValue(String content, Class[] valueType)
        throws JsonProcessingException, JsonMappingException

In the below run method, we created an instance of ObjectMapper class. The json string contains an array of student objects. We are using the readValue to parse the json string as an Array of Student objects.

@SpringBootApplication
public class JacksonFieldNameApplication implements CommandLineRunner {
	public static void main(String[] args) {
		SpringApplication.run(JacksonFieldNameApplication.class, args);
	}
	@Override
	public void run(String... args) throws Exception {
		ObjectMapper mapper = new ObjectMapper();
		String json = "[{\"id\":834567,\"studentName\":\"Jackson\"}, "
				+ "{\"id\":904562,\"studentName\":\"Michael\"}]";
		Student[] studentArray = mapper.readValue(json, Student[].class);
		for (Student temp: studentArray) {
			System.out.println(temp);
		}
	}
}

Running the above code would print the following array of Java objects:

Student [id=834567, studentName=Jackson]
Student [id=904562, studentName=Michael]

3. Jackson parse as List of Java objects

Similarly, you can parse the JSON array as a List of Java objects using the below readValue method:

public <T> T readValue(String content, TypeReference<T> valueTypeRef)
        throws JsonProcessingException, JsonMappingException

Here, the TypeReference is a class of Jackson library that allows to provide the type information to Jackson. Therefore, Jackson resolves to the provided type.

In the below code, we are passing the List<Student> as type to the readValue. So, the Jackson converts the json to a List of Student objects.

String json = "[{\"id\":834567,\"studentName\":\"Jackson\"}, " +
				  "{\"id\":904562,\"studentName\":\"Michael\"}]";
List<Student> asList = mapper.readValue(json, new TypeReference<List<Student>>() {});
		
for (Student temp: asList) {
       System.out.println(temp);
}

Alternatively, you can use the overloaded readValue function that accepts the JavaType as input:

public <T> T readValue(String content, JavaType valueType)
        throws JsonProcessingException, JsonMappingException

The JavaType is the base class that contains type information for deserializer. To create a JavaType, you first need to get the configured and readily available TypeFactory instance from the ObjectMapper object.

TypeFactory typeFactory = mapper.getTypeFactory();

You can create the CollectionType (subclass of JavaType) to deserialize the JSON array as a List of Student objects.

typeFactory.constructCollectionType(List.class, Student.class)

After creating the CollectionType, pass it to the readValue method.

String json = "[{\"id\":834567,\"studentName\":\"Jackson\"}, " +
				  "{\"id\":904562,\"studentName\":\"Michael\"}]";
TypeFactory typeFactory = mapper.getTypeFactory();
List<Student> asList = mapper.readValue(json, typeFactory
			.constructCollectionType(List.class, Student.class));
    
for (Student temp: asList) {
	System.out.println(temp);
}

Running the above code would produce the following results:

Student [id=834567, studentName=Jackson]
Student [id=904562, studentName=Michael]

4. Jackson parse as List of LinkedHashMap objects

If you pass the List.class as expected type to the readValue method, then the Jackson deserializes the JSON Array as a List of LinkedHashMap objects.

public List<LinkedHashMap> readValue(String content, List.class)
        throws JsonProcessingException, JsonMappingException

In the below code, Jackson parse the json string as an array of LinkedHashMap objects.

String json = "[{\"id\":834567,\"studentName\":\"Jackson\"}, " +
				  "{\"id\":904562,\"studentName\":\"Michael\"}]";
List<LinkedHashMap> asList = mapper.readValue(json, List.class);
		
for (LinkedHashMap temp: asList) {
	System.out.println(temp instanceof LinkedHashMap);
	System.out.println(temp);
}

Running the above code produces the result:

{id=834567, studentName=Jackson}
{id=904562, studentName=Michael}

5. Conclusion

To sum up, we have seen the various ways to convert a JSON array to array or list of Java objects.

Leave a Reply

Your email address will not be published. Required fields are marked *