How can we convert a JSONArray to String Array in Java?



A JSONArray is a class provided by the org.json package that represents a collection of JSON values. These values can be of any type, such as strings, numbers, booleans, or even nested objects or arrays. If you do not know what JSON is, then you can read the JSON tutorial.

Converting JSON Array to String Array

We can convert a JSONArray to String Array by using a simple loop as shown in the example below -

import org.json.*;
import java.util.*;
public class JsonArraytoStringArrayTest {
   public static void main(String[] args) {
      JSONArray jsonArray = new JSONArray();
      jsonArray.put("INDIA ");
      jsonArray.put("AUSTRALIA ");
      jsonArray.put("SOUTH AFRICA ");
      jsonArray.put("ENGLAND ");
      jsonArray.put("NEWZEALAND ");
      List < String > list = new ArrayList < String > ();
      for (int i = 0; i < jsonArray.length(); i++) {
         list.add(jsonArray.getString(i));
      }
      System.out.print("JSONArray: " + jsonArray);
      System.out.print("\n");
      String[] stringArray = list.toArray(new String[list.size()]);
      System.out.print("String Array: ");
      for (String str: stringArray) {
         System.out.print(str);
      }
   }
}

Output

JSONArray: ["INDIA ","AUSTRALIA ","SOUTH AFRICA ","ENGLAND ","NEWZEALAND "]
String Array: INDIA AUSTRALIA SOUTH AFRICA ENGLAND NEWZEALAND
Manisha Chand
Manisha Chand

Words That Decode Code

Updated on: 2025-06-06T12:43:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements