You can use the GSON library to accomplish this.

 ArrayList<String> mylist = new ArrayList<String> ();
 mylist.add("abc");
 mylist.add("cfd");
 mylist.add("ert");
 mylist.add("fg");
 mylist.add("ujk");
 String json = new Gson().toJson(mylist);

You can refer to the GSON User Guide for more support.

Answer from rageandqq on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-java
How to convert Java array or ArrayList to JsonArray using Gson in Java?
public JsonElement toJsonTree(java.lang.Object src) import com.google.gson.*; import java.util.*; public class JavaArrayToJsonArrayTest { public static void main(String args[]) { String[][] strArray = {{"elem1-1", "elem1-2"}, {"elem2-1", "elem2-2"}}; ArrayList<ArrayList<String>> arrayList = new ArrayList<>(); for(int i = 0; i < strArray.length; i++) { ArrayList<String> nextElement = new ArrayList<>(); for(int j = 0; j < strArray[i].length; j++) { nextElement.add(strArray[i][j] + "-B"); } arrayList.add(nextElement); } JsonObject jsonObj = new JsonObject(); // array to JsonArray JsonArray jsonArray1 = new Gson().toJsonTree(strArray).getAsJsonArray(); // ArrayList to JsonArray JsonArray jsonArray2 = new Gson().toJsonTree(arrayList).getAsJsonArray(); jsonObj.add("jsonArray1", jsonArray1); jsonObj.add("jsonArray2", jsonArray2); System.out.println(jsonObj.toString()); } }
Discussions

android - convert ArrayList to JSONArray - Stack Overflow
That got me close, but what happened was that the toString() output from each object was inserted into the JSONArray as a String. I still ended up with an JSONArray of Strings and not Objects. I ended up creating a method on my class called getJSONObject, then looped over the ArrayList and put ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Powershell arraylist to json
That would produce the JSON you want. If you have a string and need an array then use the Split operator to produce an array. More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
2
0
Convert normal Java Array or ArrayList to Json Array in android - Stack Overflow
Is there any way to convert a normal Java array or ArrayList to a Json Array in Android to pass the JSON object to a webservice? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I save an ArrayList<(Custom object here)> with JSON?
Gson gson = new Gson(); String jsonAsString = gson.toJson(arrayList); This doesn't make a JSON object, just a string in JSON format. Easier to keep up with (e.g. saving it in SharedPreferences). And to deserialize it from JSON... Type type = new TypeToken>(){}.getType(); ArrayList courses = gson.fromJson(jsonAsString, type); The GSON library is really one of the more useful tools Google has for this purpose. More on reddit.com
๐ŸŒ r/androiddev
15
3
July 21, 2014
๐ŸŒ
Crunchify
crunchify.com โ€บ json tutorials โ€บ in java how to convert arraylist to jsonobject?
In Java How to Convert ArrayList to JSONObject? โ€ข Crunchify
May 17, 2018 - Gson gson = gsonBuilder.create(); String JSONObject = gson.toJson(crunchify); log("\nConverted JSONObject ==> " + JSONObject); Gson prettyGson = new GsonBuilder().setPrettyPrinting().create(); String prettyJson = prettyGson.toJson(crunchify); log("\nPretty JSONObject ==> " + prettyJson); } private static void log(Object print) { System.out.println(print); } } Raw ArrayList ===> [Google, Facebook, Crunchify, Twitter, Snapchat, Microsoft] Converted JSONObject ==> ["Google","Facebook","Crunchify","Twitter","Snapchat","Microsoft"] Pretty JSONObject ==> [ "Google", "Facebook", "Crunchify", "Twitter", "Snapchat", "Microsoft" ]
๐ŸŒ
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 - Hi I have two lists Edge1List and Edge2List which I add to element on them. Edge1List:[o,p] and Edge2List: [p,null]. I convert Edge1List and Edge2List to JsonArray with the name of EdgeJsonArray1 and EdgeJsonArray2 respectively.
Top answer
1 of 2
1

Your description doesn't match your post title. What is your actual question?

Both Powershell and JSON don't particularly care about the structure of your data. In PS you can have an array of various objects (it sees everything as object anyway) and JSON allows for optional fields so the missing fields in the first value won't impact the structure.

# Using a hash object here for convenience
$item1 = @{ basePrice = 100; category = "some category"; packageWeightUnit = "g"; productType = "Some type" }
$item2 = @{ basePrice = 100; category = "some category"; packageWeightUnit = "g"; productType = "Some type"; attributes = @{ colour = @("blue", "red"); hight = @(100) } } 

# Assuming you have more items than this...
$list = New-Object System.Collections.ArrayList
$list += $item1
$list += $item2
ConvertTo-Json $list -depth 4

To go the other way, given the previous JSON

$readItems = ConvertFrom-Json $json
2 of 2
0

$list.add(@{"name"=$red.name;
"description"=$desc;
"coverImage"=$red.coverImage;
"ean"=$red.ean;
"packageWeightValue"=$red.packageWeightValue;
"packageWeightUnit"=$red.packageWeightUnit;
"basePrice"=$red.basePrice;
"vat"=$red.vat;
"stockLevel"=$red.stockLevel;
"sku"=$red.sku;
"gallery"=$galerija;
"brand"=$red.brand;
"productType"=$red.productType;
"category"=$red.category;
attributes = @{ "key features" = @($features.naziv)}

            })

and i am getting no error but output looks like
"attributes": {
"key features": "PARIS Hidratacija Balzam"
},

but it should be
"attributes": {
"key features": ["PARIS", "Hidratacija", "Balzam""
},

Find elsewhere
๐ŸŒ
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 addition to external libraries like Gson and Jackson, Java also provides a built-in library called org.json for JSON processing. To convert a Java List to a JSON array using org.json, we can use the JSONArray class to accomplish this task.
๐ŸŒ
GitHub
gist.github.com โ€บ 21ec4a3a1fe9dc52e2cd195479428f98
JAVA array or ArrayList to JsonArray (Gson) ยท GitHub
JAVA array or ArrayList to JsonArray (Gson). GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-we-convert-a-list-to-the-json-array-in-java
How can we convert a list to the JSON array in Java?
April 22, 2025 - First, import the Gson and its JsonArray class. Then, create a list of strings. Then we will create an instance of the Gson class. Next, we will convert the list to a JSON array using the toJsonTree() method of Gson.
๐ŸŒ
Reddit
reddit.com โ€บ r/androiddev โ€บ how do i save an arraylist with json?
r/androiddev on Reddit: How do I save an ArrayList<(Custom object here)> with JSON?
July 21, 2014 -
public class Semester {    
private static final String JSON_ID = "id";
private static final String JSON_TITLE = "title";
private static final String JSON_CREDITS = "credits";
private static final String JSON_GPA = "gpa";
private static final String JSON_CUMULATIVECREDITS = "cumulativecredits";
private static final String JSON_CUMULATIVEGPA = "cumulativegpa";
private static final String JSON_COURSES = "courses";

private UUID id;
private String title;
private int credits;
private double GPA;
private int cumulativeCredits;
private double cumulativeGPA;

private ArrayList<Course> courses;

public Semester() {
    id = UUID.randomUUID();
    courses = new ArrayList<Course>();
    for (int i = 0; i < 6; i++) {
        courses.add(new Course());
    }
}

public Semester(JSONObject json) throws JSONException {
    id = UUID.fromString(json.getString(JSON_ID));
    title = json.getString(JSON_TITLE);
    credits = json.getInt(JSON_CREDITS);
    GPA = json.getDouble(JSON_GPA);
    cumulativeCredits = json.getInt(JSON_CUMULATIVECREDITS);
    cumulativeGPA = json.getDouble(JSON_CUMULATIVEGPA);

//what do i put here?

}

public JSONObject toJSON() throws JSONException {
    JSONObject json = new JSONObject();
    json.put(JSON_ID, id.toString());
    json.put(JSON_TITLE, title);
    json.put(JSON_CREDITS, credits);
    json.put(JSON_GPA, GPA);
    json.put(JSON_CUMULATIVECREDITS, cumulativeCredits);
    json.put(JSON_CUMULATIVEGPA, cumulativeGPA);

JSONArray array = new JSONArray(courses);

json.put(JSON_COURSES, array);

    return json;
}

.........

This is followed by a bunch of getters and setters for each private variable.

  • The first bolded item is the private ArrayList of a custom class that I made.

  • The second bolded item is what I need help with: how to retrieve the custom ArrayList just like the other variables.

  • Lastly, the third bolded item is my current idea of how to put an ArrayList into a JSONObject, but I don't know how to retrieve it. If there is another way, please tell me. Thank you.

๐ŸŒ
Quora
quora.com โ€บ How-do-I-convert-a-list-to-JSON-in-Java
How to convert a list to JSON in Java - Quora
Answer (1 of 8): Just use ObjectMapper from fasterxml library. [code]import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(anyObject); [/code]
๐ŸŒ
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 - 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.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 669475 โ€บ open-source โ€บ Convert-ArrayList-custom-objects-JSON
Convert ArrayList of custom objects to JSON (Open Source Projects forum at Coderanch)
August 21, 2016 - I tried doing something like and But it seems that they work only if a String ArrayList is passed. ... What JSON library are you using? Are you converting just the list or is the list inside a class? I found the Jackson library fro JSON conversion pretty simple to use.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2019 โ€บ 07 โ€บ convert-list-to-json-array-using-jackson.html
Convert List to JSON Array Using Jackson
July 21, 2019 - The following example shows how to convert List object to JSON array using the ObjectMapper.writeValueAsString() method. package net.javaguides.jackson; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; /** * Using Jackson API for list serialization and deserialization * @author ramesh fadatare * */ public class JacksonListToJson { public static void main(String[] args) throws JsonProcessingException { // Create ObjectMapper object.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_arrays.asp
JSON Arrays
In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined.