Use GSON library for that. Here is the sample code

List<String> foo = new ArrayList<String>();
foo.add("A");
foo.add("B");
foo.add("C");

String json = new Gson().toJson(foo );

Here is the maven dependency for Gson

<dependencies>
    <!--  Gson: Java to Json conversion -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Or you can directly download jar from here and put it in your class path

http://code.google.com/p/google-gson/downloads/detail?name=gson-1.0.jar&can=4&q=

To send Json to client you can use spring or in simple servlet add this code

response.getWriter().write(json);

Answer from code_fish on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java list › converting a java list to a json array
Converting a Java List to a Json Array | Baeldung
June 18, 2025 - In the above test method, we create a Gson instance (gson) and then use the toJson() method to convert the articles List to a JSON array represented as a string. Finally, we utilize the Assert class to verify the equality of the output jsonArray ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-a-list-to-json-array-using-the-jackson-library-in-java
How to convert a List to JSON array using the Jackson library in Java?
April 29, 2025 - ArrayNode: It is a special type of JsonNode used for handling JSON arrays, allows to create, add, and modify elements manually inside a JSON array structure. ... We are using the Jackson library to convert a Java List to a JSON array and if you're using Maven, we need to add Jackson dependency to your project's classpath or pom.xml.
🌐
Quora
quora.com › How-do-I-convert-a-list-to-JSON-in-Java
How to convert a list to JSON in Java - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
🌐
GeeksforGeeks
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
July 23, 2025 - Here we are adding the logic of converting an ArrayList of objects to a JSON array 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; import java.util.List; public class Main { public static void main(String[] args) { // Create an ArrayList of objects List<Course> courses = new ArrayList<>(); courses.add( new Course(1, "Java", "25000", "3 Months")); courses.add( new Course(2, "C++", "20000", "3 Months")); // Convert the ArrayList to a JSON array ObjectMapper objectMapper = new ObjectMapper(); try { String jsonArray = objectMapper.writeValueAsString(courses); System.out.println(jsonArray); } catch (JsonProcessingException e) { e.printStackTrace(); } } }
🌐
DevGenius
blog.devgenius.io › how-to-convert-a-java-list-to-a-json-array-a-simple-guide-d3428951a488
How to Convert a Java List to a JSON Array: A Simple Guide | by M Business Solutions | Dev Genius
December 5, 2024 - Fortunately, converting a Java List to a JSON array is straightforward with the help of libraries like Jackson or Gson.
🌐
Coderanch
coderanch.com › t › 713115 › java › conversion-array-list-Json-object
conversion of array list to Json object string (Java in General forum at Coderanch)
July 19, 2019 - You need to create separate instances of JSONObject for each edge, not reuse the same one for both.
Find elsewhere
🌐
Java Guides
javaguides.net › 2019 › 07 › convert-list-to-json-array-using-jackson.html
Convert List to JSON Array Using Jackson
July 21, 2019 - In this quick article, I will show how to convert a List to JSON array using Jackson. Check out complete Jackson tutorial at Java Jackson JSON Tutorial with Examples. We are using Jackson library to convert Java List to JSON array so let's add below Jackson dependency to your project's classpath ...
🌐
GitHub
gist.github.com › sheharyarn › cba56ff154de2cc62fc5
Simple Java Class to convert Lists/Maps to JSON and vice versa · GitHub
Simple Java Class to convert Lists/Maps to JSON and vice versa · Raw · SimpleJSON.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
YouTube
youtube.com › watch
How To Convert Java List Object Into JSON String using Jackson API | Java Inspires - YouTube
In this video, we will see how to convert(serialize) Java List Object into JSON String and vice versa means how to convert(deserialize) JSON String to Java ...
Published: June 7, 2021
🌐
daily.dev
daily.dev › posts › converting-a-java-list-to-a-json-array-pgncmsxjd
Converting a Java List to a Json Array | daily.dev
June 15, 2023 - Converting a Java List to a Json Array is a common task in modern software development. In this tutorial, we'll explore different approaches to achieve this...
🌐
How to do in Java
howtodoinjava.com › home › convert json array to list in java: jackson, gson, org.json
Convert JSON Array to List in Java: Jackson, Gson, Org.json
2 weeks ago - To convert a JSON array to a list in Java, we parse the array with a JSON library and tell the library the element type, such as new TypeReference<List<Book>>() {} in Jackson or new TypeToken<List<Book>>() {} in Gson.
🌐
Dejavuhyo
dejavuhyo.github.io › posts › list-to-json-convert
List를 Json으로 변환 | 공대베짱이
January 12, 2023 - 1. Gson Download Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
🌐
Makeinjava
makeinjava.com › home › convert list of objects to/from json in java (jackson objectmapper/ example)
Convert list of objects to/from JSON in java (jackson objectmapper/example)
January 1, 2024 - We will use the jackson’s objectmapper, to serialize list of objects to JSON & deserialize JSON to List of objects.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting JSON Array to Java List with Gson - Java Code Geeks
April 25, 2024 - In the realm of modern software development, the manipulation of data interchange formats like JSON (JavaScript Object Notation) is ubiquitous. Converting JSON Array into Java List objects emerges as a frequent necessity for Java developers striving for efficient data processing. Gson, a cornerstone Java library for JSON parsing, stands ready to streamline this conversion process.
Top answer
1 of 2
2

Iterate through the list in reverse and upon each iteration create a new JsonNode for the list element, add the previous JsonNode as a child to the new JsonNode and overwrite the original reference with the new JsonNode. Rinse and repeat.

Something like...

final String value = "BMW"; // whatever the final "value" is.
final ObjectMapper objectMapper = new ObjectMapper();

ObjectNode json = null;

for (int i = list.size() - 1; i >= 0; i--) {

  final String prop = list.get(i);
  final ObjectNode objectNode = objectMapper.createObjectNode();

  if (json == null) {
    objectNode.put(prop, value);
  } else {
    objectNode.put(prop, json);
  }

  json = objectNode;
}
2 of 2
-1

Here is the piece of code which you need

package uk.co.vishal;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Answer {

public static void main(String[] args) throws JsonProcessingException {
    List<String> data = new ArrayList<>();
    data.add("user");
    data.add("car");
    data.add("brand");

    System.out.println(data.toString());

    Map<String, String> mydata = new HashMap<>();
    Map<String, JSONObject> newdata = new HashMap<>();
    JSONObject result = new JSONObject();
    boolean flag = true;

    for (int i = data.size() - 1; i >= 0; i--) {
        if (flag) {
            mydata.put(data.get(i), "BMW");
            result = generateJson(mydata);
            flag = false;
        } else {
            newdata.put(data.get(i), result);
            result = generateJson(newdata);
            newdata.clear();
        }
    }
    System.out.println(result);
    /*
    * Here is the output:
    * {
        "user": {
            "car": {
                    "brand": "BMW"
                }
            }
      }
    *
    * */
}

private static JSONObject generateJson(Map data) {
    return new JSONObject(data);
}

}

I hope you will be sorted !!