VOOZH about

URL: https://www.geeksforgeeks.org/java/how-to-convert-an-arraylist-of-objects-to-a-json-array-in-java/

⇱ How to Convert an ArrayList of Objects to a JSON Array in Java? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Convert an ArrayList of Objects to a JSON Array in Java?

Last Updated : 23 Jul, 2025

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 of objects to a JSON array in Java.

Steps to Convert an ArrayList of Objects to a JSON Array in Java

Below are the steps and implementation of converting an ArrayList of objects to a JSON array with Jackson.

Step 1: Create a Maven Project

Open any one 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.

👁 Maven Project Creation

Project Structure:

Below is the Project Structure that we have created.

👁 Project Structure

Step 2: Add Jackson Dependency to pom.xml

Now, we will add Jackson dependency to XML file.

Step 3: In the Main Class Add Logic of Converting an ArrayList of Objects to a JSON Array of Java

Now we will create a POJO class named Course.Java.

Create a Course class: Course.Java

In the above code, a Java class called Course models the properties of a course like its id, name, fees, and duration. It has getter and setter methods to access and modify the private fields.

Create a Main Class:

Here we are adding the logic of converting an ArrayList of objects to a JSON array of Java in the main class.

Output:

[ {"id":1 , "course_name":"Java" , "course_fees" : "25000" , "course_duration":"3 Months" } , 
{"id":2 , "course_name":"C++" , "course_fees" : "20000" , "course_duration":"3 Months" } ]

Explanation of the above code:

  • In the above code, it first creates an ArrayList and adds two Course objects to it.
  • Then it initializes an ObjectMapper from Jackson to convert objects to JSON.
  • The writeValueAsString method converts the ArrayList to a JSON string which is printed. Any exceptions during conversion are caught and printed.
Comment