Java Jackson – Convert JSON array string to List

1. Introduction

Jackson is one of the most commonly used libraries for processing JSON in Java. Not only can it parse JSON objects, but it can also efficiently handle JSON arrays. This guide will demonstrate how to convert a JSON array string into a Java List using Jackson.

2. Example Steps

1. Ensure Jackson library dependencies are added to your project.

2. Prepare a JSON array string that you want to convert.

3. Utilize Jackson's ObjectMapper class to convert the JSON array string into a Java List.

4. Display the parsed data in the List.

3. Java Jackson Convert JSON array string to List

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

import java.util.List;

public class JacksonJsonArrayToListExample {

    public static void main(String[] args) {
        // Sample JSON array string.
        String jsonArray = "[\"apple\", \"banana\", \"cherry\"]";

        try {
            // Initialize ObjectMapper instance.
            ObjectMapper objectMapper = new ObjectMapper();

            // Convert JSON array string to Java List.
            List<String> fruitList = objectMapper.readValue(jsonArray, new TypeReference<List<String>>(){});

            // Print out the List.
        } catch (JsonProcessingException e) {


[apple, banana, cherry]

4. Step By Step Explanation

1. We begin by setting up a sample JSON array string that contains a list of fruits.

2. The ObjectMapper class, a key component from Jackson, is initialized. It offers functionalities for binding JSON data to Java objects.

3. To convert the JSON array string into a Java List, we use the readValue method of ObjectMapper. We specify a TypeReference to inform Jackson about the data type within the List (in this case, a List of Strings).

4. Finally, after obtaining the data in the List format, it's printed to the console. The output confirms that the JSON array string was successfully transformed into a Java List.