How to Convert an ArrayList into a JSON String in Java?
In Java, an ArrayList is a resizable array implementation of the List interface. It implements the List interface and is the most commonly used implementation of List. In this article, we will learn how to convert an ArrayList into a JSON string in Java.
Steps to convert an ArrayList into a JSON string in Java
Below are the steps and implementation of converting an ArrayList into a JSON string with Jackson.
Step 1: Create a Maven Project
Open any preferred IDE and create a new Maven project. Here we are using IntelliJ IDEA, so, we can do this by selecting File -> New -> Project.. -> Maven and following the wizard.

Project Structure:
Below is the project structure that we have created.

Step 2: Add Jackson Dependency to pom.xml
Now, we will add Jackson dependency to pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>ArrayList</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.5</version> <!-- Replace with the latest version -->
</dependency>
</dependencies>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Step 3: Add logic of converting an ArrayList into a JSON string
Here we are adding the logic of converting an ArrayList into a JSON string of Java in the main class.
package org.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
public class Main {
public static void main(String[] args)
{
// Create an ArrayList
ArrayList<String> courses = new ArrayList<>();
courses.add("Java");
courses.add("Python");
courses.add("C++");
// Convert ArrayList to JSON string
ObjectMapper objectMapper = new ObjectMapper();
try {
String jsonString
= objectMapper.writeValueAsString(courses);
// Print JSON string
System.out.println(jsonString);
}
catch (JsonProcessingException e) {
e.printStackTrace();
}
}
}
Output:

Explanation of the above Program:
- It creates an ArrayList of type String and adds 3 course names to it.
- It uses the ObjectMapper from Jackson to convert the ArrayList to a JSON string.
- It catches any exceptions that may occur during the JSON conversion.
- It prints out the JSON string representation of the ArrayList to the console.