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)
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values). It also provides an unmodifiable list view of the values in the array.
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
February 25, 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
November 28, 2021
Get a random object from a array which is in a JSON file

You can select a random object from an array like this,

const quotes = [{},..];

const quote = quotes[Math.floor(Math.random() * quotes.length)];

Now, quote will contain a random object from the quotes array.

More on reddit.com
🌐 r/reactjs
5
3
December 11, 2016
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
JSON Arrays
Arrays in JSON are almost the same as arrays in JavaScript.
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
Append a boolean value. This increases the array's length by one. ... Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - CDL – a tool that provides methods to convert comma-delimited text into a JSONArray and vice versa · Cookie – converts from JSON String to cookies and vice versa · HTTP – used to convert from JSON String to HTTP headers and vice versa · JSONException – a standard exception thrown by this library · A JSONObject is an unordered collection of key and value pairs, resembling Java’s native Map implementations.
🌐
Esri Developer
developers.arcgis.com › enterprise-sdk › api-reference › java › com › esri › arcgis › server › json › JSONArray.html
JSONArray (ArcObjects Java API)
public JSONArray put(int index, java.lang.Object value) throws JSONException · Put or replace an object value in the JSONArray. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out. ... value - The value to put into the array.
Find elsewhere
🌐
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 - In essence, ArrayNode is suitable for structured JSON arrays, while JsonNode is ideal for complex, dynamic JSON processing. One of Jackson’s strongest features is its ability to easily convert JSON data into Java objects, and vice versa.
🌐
RestfulAPI
restfulapi.net › home › json › json array
JSON Array - Multi-dimensional Array in JSON
November 4, 2023 - For example, given below is a JSON document that contains a JSON array of access rights. { "name" : "Admin", "age" : 36, "rights" : [ "admin", "editor", "contributor" ] } You can access the array values by using the index number: ... Program output. ... Program output. ... We can store an array inside another JSON array.
🌐
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
🌐
Javadoc.io
javadoc.io › static › org.json › json › 20171018 › index.html
JSON in Java 20171018 API
JavaScript is disabled on your browser · Frame Alert · This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to Non-frame version
🌐
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 ...
🌐
JSON
json.org
JSON
It makes sense that a data format ... }right brace. Each name is followed by :colon and the name/value pairs are separated by ,comma. An array is an ordered collection of values....
🌐
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 · 中文 – 简体
🌐
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.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting a JSON Object to a JSON Array in Java - Java Code Geeks
August 1, 2025 - This Java code demonstrates how to convert a JSON object into a JSON array of key-value pair objects using the org.json library. It begins by defining a JSON string containing fruit names as keys and their quantities as values. This string is parsed into a JSONObject.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › org.json.jsonarray
JSONArray Class (Org.Json) | Microsoft Learn
A dense indexed sequence of values. [Android.Runtime.Register("org/json/JSONArray", DoNotGenerateAcw=true)] public class JSONArray : Java.Lang.Object
🌐
Coderanch
coderanch.com › t › 694185 › open-source › reading-JSON-array
Help reading JSON array [Solved] (Open Source Projects forum at Coderanch)
May 15, 2018 - I think jsonObject.get() is returning an JSONArray which contains an array of Long objects. To transform the JSONArray to an long[], you could do something like this: or maybe this: For example, building on your code: ... Dear Mr. Mike, You cannot be converted into integer array directly as mentioned in your code.
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.
🌐
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 - For example, storing a list of items, user profiles, product catalog, etc. JSON arrays allow ordered access to the values using indices like in regular arrays in other languages (0 indexed). ... Iterating and parsing manually. Below are the steps and implementation to convert JSON Array object to Java object using Jackson library.
🌐
LabEx
labex.io › tutorials › java-how-to-create-json-arrays-420797
How to create JSON arrays | LabEx
This comprehensive tutorial explores the essential techniques for creating JSON arrays in Java, providing developers with practical insights into JSON array manipulation.