Here is some code using java 6 to get you started:

JSONObject jo = new JSONObject();
jo.put("firstName", "John");
jo.put("lastName", "Doe");

JSONArray ja = new JSONArray();
ja.put(jo);

JSONObject mainObj = new JSONObject();
mainObj.put("employees", ja);

Edit: Since there has been a lot of confusion about put vs add here I will attempt to explain the difference. In java 6 org.json.JSONArray contains the put method and in java 7 javax.json contains the add method.

An example of this using the builder pattern in java 7 looks something like this:

JsonObject jo = Json.createObjectBuilder()
  .add("employees", Json.createArrayBuilder()
    .add(Json.createObjectBuilder()
      .add("firstName", "John")
      .add("lastName", "Doe")))
  .build();
Answer from Grammin on Stack Overflow
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
If the value at the specified position is JsonValue.TRUE this method returns true. If the value at the specified position is JsonValue.FALSE this method returns false. Otherwise this method returns the specified default value. ... Returns true if the value at the specified location in this array is JsonValue.NULL.
Discussions

How to turn json objects into an array of json objects using Java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
7
4
June 28, 2022
New to Java, wanted some help with reading .json object arrays.
it gives me an error What's the error? More on reddit.com
🌐 r/javahelp
11
1
January 16, 2024
using JSONArray, JSONObject etc. to parse JSON with Java
I'd highly advise you to use the ObjectMapper instead of JSONArray and JSONObject. Try to mimic the json objects structure with a class, create 0 field and all argument contrstructors, getters and setters and simply use new ObjectMapper().readValue(yourJsonString, YouMappedObject.class) More on reddit.com
🌐 r/javahelp
6
5
December 4, 2021
How to parse Json array with 2 or more different types using Jackson?

http://www.baeldung.com/jackson-inheritance

You'll need to have those types inherit from one base class though.

Also; that is really bad JSON.

More on reddit.com
🌐 r/javahelp
11
4
September 21, 2016
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
NullPointerException - Thrown if the array parameter is null. public JSONArray(int initialCapacity) throws JSONException
🌐
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 this scenario, let’s consider a Java list named “articles” that contains elements as follows: public List<String> list = Arrays.asList("Article 1", "Article 2", "Article 3"); public String expectedJsonArray = "[\"Article 1\",\"Article ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - A JSON array is an ordered collection of values that are enclosed in square brackets i.e., it begins with '[' and ends with ']'. The values in the arrays are separated by ',' (comma).Sample JSON array{ "books": [ Java, JavaFX, Hbase, Cassandra, We
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-parse-json-array-using-java
How to read/parse JSON array using Java?
May 5, 2025 - The iterator() method of the JSONArray class returns an Iterator object, using which you can iterate through the contents of the current array. Iterator<String> iterator = jsonArray.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } Below is an example of a Java program that parses the above-created sample.json file, reads its contents, and displays them.
🌐
Coderanch
coderanch.com › t › 706235 › java › Identifying-JSONObject-JSONArray
Identifying JSONObject or JSONArray (Java in General forum at Coderanch)
Here are partial examples: This is the normal string that works: This is the response that causes the parser exception: ... Sam Ritter wrote:... is there a way to determine which sb is before running the parser? The easiest way is probably to use the parser to determine if JSON element is what you are expecting - as you have found, it will throw an exception if it is the wrong type of element.
Find elsewhere
🌐
JanBask Training
janbasktraining.com › community › java › how-to-create-correct-jsonarray-in-java-using-jsonobject
How to create correct JSONArray in Java using JSONObject | JanBask Training Community
November 2, 2025 - JSONObject obj1 = new JSONObject(); obj1.put("id", 1); obj1.put("name", "Alice"); JSONObject obj2 = new JSONObject(); obj2.put("id", 2); obj2.put("name", "Bob"); ... You can loop through data and dynamically put objects into the array.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting a JSON Object to a JSON Array in Java - Java Code Geeks
August 1, 2025 - To convert this object into a JSON array, the code first transforms the JSONObject into a Map using toMap(), then extracts only the values from that map using values(), and wraps those values in a new JSONArray.
🌐
Crunchify
crunchify.com › json tutorials › how to parse jsonobject and jsonarrays in java? beginner’s guide
How to Parse JSONObject and JSONArrays in Java? Beginner's Guide • Crunchify
February 2, 2023 - Here is a simple Java tutorial which demonstrate how to parse JSONObject and JSONArrays in Java. JSON syntax is a subset of the JavaScript object notation
🌐
Medium
medium.com › @Mohd_Aamir_17 › mastering-json-in-java-a-comprehensive-guide-to-handling-json-objects-arrays-and-nodes-with-df57bf0ebff1
Mastering JSON in Java: A Comprehensive Guide to Handling JSON Objects, Arrays, and Nodes with Jackson | by Mohd Aamir | Medium
November 4, 2024 - Whether you’re developing REST APIs, handling data transformation, or integrating with third-party services, understanding how to handle JSON data efficiently in Java is crucial. This article offers an in-depth look at how to parse, create, and manipulate JSON objects, arrays, and nodes using the Jackson library, covering advanced scenarios to equip you with the tools to tackle complex JSON data in Java.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-json-array-to-string-array-in-java
How to Convert JSON Array to String Array in Java? - GeeksforGeeks
July 23, 2025 - Let's start with creating a JSON array in Java. Here, we will use some example data to input into the array, but you can use the data as per your requirements.
🌐
Android Developers
developer.android.com › api reference › jsonarray
JSONArray | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-json-array-object-to-java-object
How to Convert JSON Array Object to Java Object? - GeeksforGeeks
July 23, 2025 - Now, we will add the logic to convert JSON Array Object to Java Object in the main class. ... package org.example; import com.fasterxml.jackson.databind.ObjectMapper; public class Main { public static void main(String[] args) { try { // JSON string representing an array of user objects String jsonString = "[{\"firstName\":\"User1\",\"lastName\":\"XYZ\",\"email\":\"user1@example.com\"}," + "{\"firstName\":\"User2\",\"lastName\":\"PQR\",\"email\":\"user2@example.com\"}]"; // Create an ObjectMapper instance ObjectMapper objectMapper = new ObjectMapper(); // Deserialize the JSON string into an arr
🌐
GitHub
github.com › stleary › JSON-java › blob › master › src › main › java › org › json › JSONArray.java
JSON-java/src/main/java/org/json/JSONArray.java at master · stleary/JSON-java
private void addAll(Collection<?> collection, boolean wrap, int recursionDepth, JSONParserConfiguration jsonParserConfiguration) { this.myArrayList.ensureCapacity(this.myArrayList.size() + collection.size()); ... private void ...
Author   stleary
🌐
ReqBin
reqbin.com › json › java › uzykkick › json-array-example
Java | What is JSON Array?
June 11, 2022 - Unlike dictionaries, where you can get the value by its key, in a JSON array, the array elements can only be accessed by their index. The following is an example of a JSON array with numbers. Below, you can find a list of JSON arrays with different data types. The Java code was automatically ...
🌐
Javatpoint
javatpoint.com › json-array
JSON Array - javatpoint
JSON Array for beginners and professionals with examples of JSON with java, json array of string, json array of numbers, json array of booleans, json srray of objects, json multidimentional array. Learn JSON array example with object, array, schema, encode, decode, file, date etc.
🌐
Reddit
reddit.com › r/javahelp › how to turn json objects into an array of json objects using java?
r/javahelp on Reddit: How to turn json objects into an array of json objects using Java?
June 28, 2022 -

Hi. I'm new to java and stuck wondering how I would go about turning something like this: {} {} {} into this: [{},{},{}]. I'm trying to figure out how to do this to my incoming json data using Java. The data has like 20 similar objects, with the same keys but with constantly changing values.

Any help is much appreciated.

Sincerely,

your friendly neighborhood noob

Top answer
1 of 2
4
Look into Collections. you can use a List with List.of(obj1, obj2,...), The return here is going to be immutable and serializable, alternatively Arrays.asList({obj1, obj2,...}). Again, check out java.util.collections for more details
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Coderanch
coderanch.com › t › 694185 › open-source › reading-JSON-array
Help reading JSON array [Solved] (Open Source Projects forum at Coderanch)
May 15, 2018 - The following code reads the "Control" JSON element okay, but gets the following error when reading the Zones array element (the error occurs at the last code line):. Java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to and Zones and (Integer and (Integer ).
🌐
Cloudera Community
community.cloudera.com › t5 › Support-Questions › Converting-JSON-to-Java-Object-Array › m-p › 153155
Solved: Converting JSON to Java Object Array - Cloudera Community - 153155
February 1, 2017 - Here is the correct example to parse your JSON. import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class JSONParser { public static void main(String[] args) throws IOException { try(Reader reader = new InputStreamReader(JsonToJava.class.getResourceAsStream("Server1.json"), "UTF-8")){ Gson gson = new GsonBuilder().create(); Person p = gson.fromJson(reader, Person.class); System.out.println(p); } } }