This way is worked for me:

Convert all list to String.

String element = gson.toJson(
                    groupsList,
           new TypeToken<ArrayList<GroupItem>>() {}.getType());

Create JSONArray from String:

    JSONArray list = new JSONArray(element);
Answer from snaggs on Stack Overflow
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ 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?
July 8, 2020 - 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()); } }
๐ŸŒ
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 - @Test public void given_JavaLi... jsonArray); } In the provided code snippet, we instantiate the ObjectMapper class and utilize its writeValueAsString() method....
Discussions

Parsing JSON array into java.util.List with Gson - Stack Overflow
And now I want to parse that JsonArray into a java.util.List... ... Possible duplicate of Easy way to change Iterable into Collection or Convert Iterator to ArrayList. ... Save this answer. ... Show activity on this post. Definitely the easiest way to do that is using Gson's default parsing ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to convert List to a JSON Object using GSON? - Stack Overflow
I have a List which I need to convert into JSON Object using GSON. My JSON Object has JSON Array in it. public class DataResponse { private List apps; // getters and More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - How to convert list to json using Gson? - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to convert ArrayList of custom class to JsonArray in Java? - Stack Overflow
I am trying to convert ArrayList of custom class to JsonArray. Below is my code. It executes fine but some JsonArray elements come as zeros even though they are numbers in the ArrayList. I have tri... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ gson โ€บ gson โ€“ parse json array to java array or list
Gson - Parse JSON Array to Java Array or List
April 4, 2023 - Here ArrayItem is the class type of data elements in the array. ArrayItem[] userArray = new Gson().fromJson(jsonSource, ArrayItem[].class); For converting such JSON array to a List, we need to use TypeToken class.
Top answer
1 of 6
321

Definitely the easiest way to do that is using Gson's default parsing function fromJson().

There is an implementation of this function suitable for when you need to deserialize into any ParameterizedType (e.g., any List), which is fromJson(JsonElement json, Type typeOfT).

In your case, you just need to get the Type of a List<String> and then parse the JSON array into that Type, like this:

import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

JsonElement yourJson = mapping.get("servers");
Type listType = new TypeToken<List<String>>() {}.getType();

List<String> yourList = new Gson().fromJson(yourJson, listType);

In your case yourJson is a JsonElement, but it could also be a String, any Reader or a JsonReader.

You may want to take a look at Gson API documentation.

2 of 6
19

Below code is using com.google.gson.JsonArray. I have printed the number of element in list as well as the elements in List

import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class Test {

    static String str = "{ "+ 
            "\"client\":\"127.0.0.1\"," + 
            "\"servers\":[" + 
            "    \"8.8.8.8\"," + 
            "    \"8.8.4.4\"," + 
            "    \"156.154.70.1\"," + 
            "    \"156.154.71.1\" " + 
            "    ]" + 
            "}";

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {

            JsonParser jsonParser = new JsonParser();
            JsonObject jo = (JsonObject)jsonParser.parse(str);
            JsonArray jsonArr = jo.getAsJsonArray("servers");
            //jsonArr.
            Gson googleJson = new Gson();
            ArrayList jsonObjList = googleJson.fromJson(jsonArr, ArrayList.class);
            System.out.println("List size is : "+jsonObjList.size());
                    System.out.println("List Elements are  : "+jsonObjList.toString());


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

OUTPUT

List size is : 4

List Elements are  : [8.8.8.8, 8.8.4.4, 156.154.70.1, 156.154.71.1]
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ json โ€บ jackson โ€บ convert json array to java list
Convert JSON Array to Java List | Baeldung
August 13, 2025 - Gson is a widely-used JSON library for serializing and deserializing Java objects to and from JSON.
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ gson-convert-json-array-to-from-java-list
Convert JSON Array to List using Gson in Java
October 14, 2022 - The following example demonstrates how to use the toJson() method from the Gson class to convert a list of Java Objects to their JSON representation:
๐ŸŒ
Java Code Geeks
javacodegeeks.com โ€บ home โ€บ core java
Converting JSON Array to Java List with Gson - Java Code Geeks
April 25, 2024 - We parse a JSON array into a JsonArray using Gsonโ€™s JsonParser. Then, we iterate over each element of the array, converting them into Java objects using Gsonโ€™s fromJson() method. Finally, we add each Java object to a List.
๐ŸŒ
Level Up Lunch
leveluplunch.com โ€บ java โ€บ examples โ€บ convert-json-array-to-arraylist-gson
Json array to ArrayList gson | Level Up Lunch
August 10, 2014 - @Test public void convert_json_to_array_or_list() { Gson gson = new Gson(); NavItem[] navigationArray = gson.fromJson(jsonString, NavItem[].class); logger.info(navigationArray); assertEquals(4, navigationArray.length); // or @SuppressWarnin...
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-to-convert-list-to-a-json-object-using-gson
How to Convert a List to a JSON Object with a JSON Array Using GSON โ€” javathinking.com
To achieve this, weโ€™ll use a wrapper Java class that contains the List as a field. When serialized, this class becomes the top-level JSON object, and the List field becomes the JSON array.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2013 โ€บ 04 โ€บ convert-json-array-to-string-array-list-java-from.html
How to Convert JSON array to String array in Java - GSon example
This Java example uses the GSON library to create a List of String from JSON array and further Java standard library to convert List to an array. Instead of declaring JSON array in Code, which we did here for demonstration purposes, you can also read input from a file, database, or any URL.
๐ŸŒ
Rip Tutorial
riptutorial.com โ€บ jsonarray to java list (gson library)
Java Language Tutorial => JsonArray to Java List (Gson Library)
ArrayList<String> list = new Gson().fromJson(jsonList, listType); return list; } You should add the following maven dependency to your POM.xml file: <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> ...
๐ŸŒ
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 - Gson, developed by Google, is a small library with no dependencies that maps JSON to Java objects through reflection.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ convert-a-list-of-objects-to-json-using-the-gson-library-in-java
GSON Class
April 22, 2025 - Gson is the main actor class of Google Gson library. It provides functionalities to convert Java objects to matching JSON constructs and vice versa.