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
🌐
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. JSONArray array = new JSONArray(); array.add("element_1"); array.add("element_2"); array.add("element_3"); After adding all the required elements add ...
Discussions

How to Create JSON Array in Java - Stack Overflow
I'd probably use the GSON library which will marshall Java objects. There's an array example to get you started, as well as a ton of others in the user guide. ... XStream provides a very convenient way to transform JSON to JAVA and back. Its annotation based and does the conversion job pretty well. More on stackoverflow.com
🌐 stackoverflow.com
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
Create JSON with Arrays of different types?
Let's go a bit out of order: Does indentation matter in JSON? No. The only things that matter in JSON are the few symbols it uses, it ignores whitespace outside of strings. But also: I don't know how to format it to have every variable listed in the correct spot. Order does not matter in JSON. That's why every property has a name. For the rest of it, here's a quick crash course. You didn't say how you're "making a JSON file". Usually in C# these days, we use a "serialization library". MS has one in I think the System.Text namespace, but before that existed everyone used a package called Newtonsoft.JSON. They both work roughly the same: they convert C# objects to and from JSON. So if I wrote a class like this: public class Example { public int Value { get; set; } } I'd write code (sort of, I'm not double-checking) like this with Newtonsoft: var example = new Example(); example.Value = 10; var json = JsonConvert.SerializeObject(example); The JSON I'd get in return would look like: { "Value": 10 } These libraries support a lot of types by default. We can go backwards from JSON back to C#. Suppose I saw JSON like this: { "examples": [ { "Value": 10 } ] } Think about it by reading from top to bottom. This is: An object with properties: "examples", an array of objects that have these properties: "Value", an integer I see two objects in the definitions, so I need to write two classes: // "an object with an 'examples' property" // Note the name DOES NOT MATTER, because JSON objects do not have type names. public class Examples { // "an array of objects" // Note I can use a capital letter even though the JSON used lowercase, the serializer // is smart enough to handle that. public Example[] Examples { get; set; } } // "an object with a 'Value' property" public class Example { public int Value { get; set; } } The important thing to note is the serializer is smart enough to see that since Examples has an array of Example, it should try to parse the "inner" objects to fit the Example class. My second example intentionally looks a lot like your JSON. You have an object with an "APInvoices" property that is an array of other objects. THOSE objects have many properties, including some like "Images" that are arrays of OTHER objects. To serialize/deserialize your JSON, you're going to need to write 7 or 8 total C# classes. More on reddit.com
🌐 r/csharp
7
2
October 13, 2022
streaming a large json object to a file to avoid memory allocation error

Well, JSON.stringify and JSON.parse are synchronous methods that unfortunately you can't pipe to and from. Like u/nissen2 suggests, you'll find some modules out there that will provide you what you're looking for.

Ah, I think I got it. Is there a way to permanently increase the amount of memory that node has access to? Thx.

While this may solve your problem in the mean time, I wouldn't suggest simply allocating more memory (especially with a huge JSON object.)

More on reddit.com
🌐 r/node
16
0
October 13, 2014
People also ask

Can I create a JSONArray directly without JSONObjects?
Yes, you can create a JSONArray of primitive values like strings or numbers JSONArray array = new JSONArray(); array.put(“Value1”); array.put(“Value2”);
🌐
intellipaat.com
intellipaat.com › home › blog › how to create a correct jsonarray in java using jsonobject?
How to Create a Correct JSONArray in Java Using JSONObject?
How do I parse an existing JSON string into a JSONArray?
Use the JSONArray constructor: String jsonString = “[{“key”:”value”}]”; JSONArray jsonArray = new JSONArray(jsonString);
🌐
intellipaat.com
intellipaat.com › home › blog › how to create a correct jsonarray in java using jsonobject?
How to Create a Correct JSONArray in Java Using JSONObject?
How do I convert a JSONArray to a Java List?
You can iterate over the JSONArray and populate a Java List: List list = new ArrayList(); for (int i = 0; i < jsonArray.length(); i++) { list.add(jsonArray.getJSONObject(i)); }
🌐
intellipaat.com
intellipaat.com › home › blog › how to create a correct jsonarray in java using jsonobject?
How to Create a Correct JSONArray in Java Using JSONObject?
🌐
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. A JsonArray object can be created by reading JSON data from an input source or it can be built from scratch using an array builder object.
🌐
LabEx
labex.io › tutorials › java-how-to-create-json-arrays-420797
How to create JSON arrays | LabEx
Learn efficient Java techniques for creating and manipulating JSON arrays with practical examples and methods for modern software development
🌐
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.
🌐
Kodejava
kodejava.org › how-do-i-create-jsonarray-object
How do I create JSONArray object? - Learn Java by Examples
The following example show you ... jsonArray.put("Go"); System.out.println(jsonArray); // Create JSONArray from a string wrapped in bracket and // the values separated by comma....
Find elsewhere
🌐
Attacomsian
attacomsian.com › blog › jackson-create-json-array
How to create a JSON array using Jackson
October 14, 2022 - The following example demonstrates how you can use the ObjectMapper class to produce a JSON array: try { // create `ObjectMapper` instance ObjectMapper mapper = new ObjectMapper(); // create three JSON objects ObjectNode user1 = mapper.createObjectNode(); user1.put("id", 1); user1.put("name", "John Doe"); ObjectNode user2 = mapper.createObjectNode(); user2.put("id", 2); user2.put("name", "Tom Doe"); ObjectNode user3 = mapper.createObjectNode(); user3.put("id", 3); user3.put("name", "Emma Doe"); // create `ArrayNode` object ArrayNode arrayNode = mapper.createArrayNode(); // add JSON users to ar
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray java code examples | Tabnine
HttpClient httpclient = new ... while ((line = reader.readLine()) != null) { sb.append(line + "\n"); JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSON...
🌐
TutorialsPoint
tutorialspoint.com › how-to-create-a-json-array-using-object-model-in-java
How to create a JSON Array using Object Model in Java?
import java.io.*; import javax.json.*; import javax.json.JsonObjectBuilder; public class JsonArrayTest { public static void main(String[] args) { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("Name", "Raja Ramesh"); builder.add("Designation", "Java Developer"); builder.add("Company", "TutorialsPoint"); JsonArray contactInfo = Json.createArrayBuilder().add(Json.createObjectBuilder().add("email", "raja@gmail.com")).add(Json.createObjectBuilder().add("mobile", "9959984000")).build(); builder.add("contactInfo", contactInfo); JsonObject data = builder.build(); StringWriter sw = new StringWriter(); JsonWriter jw = Json.createWriter(sw); jw.writeObject(data); jw.close(); System.out.println(sw.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 - Furthermore, web services and APIs often rely on JSON format to provide public data in a standardized manner. Its versatility makes it compatible with modern programming languages, allowing seamless integration across different platforms and technologies. 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 2\",\"Article 3\"]";
🌐
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.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArrayBuilder.html
JsonArrayBuilder (Java(TM) EE 7 Specification APIs)
A builder for creating JsonArray models from scratch. This interface initializes an empty JSON array model and provides methods to add values to the array model and to return the resulting array.
🌐
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.
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - One of JSONObject’s constructors takes a POJO as its argument. In the example below, the package uses the getters from the DemoBean class and creates an appropriate JSONObject for the same. To get a JSONObject from a Java Object, we’ll have to use a class that is a valid Java Bean:
🌐
W3Docs
w3docs.com › java
How to create correct JSONArray in Java using JSONObject
import org.json.JSONArray; import org.json.JSONObject; public class Main { public static void main(String[] args) { JSONArray array = new JSONArray(); JSONObject obj1 = new JSONObject(); obj1.put("key1", "value1"); obj1.put("key2", "value2"); JSONObject obj2 = new JSONObject(); obj2.put("key3", "value3"); obj2.put("key4", "value4"); array.put(obj1); array.put(obj2); System.out.println(array.toString()); } }
🌐
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.
🌐
IBM
ibm.com › support › pages › creating-json-string-json-object-and-json-arrays-automation-scripts
Creating a JSON String from JSON Object and JSON Arrays in Automation Scripts
# creating a JSON String with an array (directly executed via Run Automation Script button) from com.ibm.json.java import JSONObject, JSONArray from sys import * # method for creating a JSON formatted String including an array within def createJSONstring(): # defining the first child object ch1_obj = JSONObject() ch1_obj.put('CH_FIELD_1', 1) ch1_obj.put('CH_FIELD_2', 'VALUE_2') # defining the second child object ch2_obj = JSONObject() ch2_obj.put('CH_FIELD_1', 2) ch2_obj.put('CH_FIELD_2', 'VALUE_3') # adding child objects to children array ch_arr = JSONArray() ch_arr.add(ch1_obj) ch_arr.add(ch
🌐
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.