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)
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.
🌐
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
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
... Remove an index and close the hole. ... The value that was associated with the index, or null if there was no value. ... Determine if two JSONArrays are similar. They must contain similar sequences. ... Produce a JSONObject by combining a JSONArray of names with the values of this JSONArray.
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java | Baeldung
June 20, 2025 - Like JSONObject, the JSONArray also has a constructor that creates a Java object directly from a JSON String:
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
W3Schools.com
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Find elsewhere
🌐
Rip Tutorial
riptutorial.com › add jsonarray to jsonobject
Android Tutorial => Add JSONArray to JSONObject
// Create a new instance of a JSONArray JSONArray array = new JSONArray(); // With put() you can add a value to the array. array.put("ASDF"); array.put("QWERTY"); // Create a new instance of a JSONObject JSONObject obj = new JSONObject(); try { ...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting a JSON Object to a JSON Array in Java - Java Code Geeks
August 1, 2025 - // ManualJsonConversion.java import ... fruitMap.put("apple", 10); fruitMap.put("banana", 20); fruitMap.put("cherry", 30); JsonArray array = new JsonArray(); for (Map.Entry<String, Integer> entry : fruitMap.entrySet()) { JsonObject obj ...
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray java code examples | Tabnine
JSONArray array; for(int n = 0; n < array.length(); n++) { JSONObject object = array.getJSONObject(n); // do some stuff.... } ... /** * Put a value in the JSONArray, where the value will be a JSONArray which * is produced from a Collection.
🌐
Baeldung
baeldung.com › home › java › java array › convert json object to json array in java
Convert JSON Object to JSON Array in Java | Baeldung
August 24, 2025 - JSONArray convertToEntryArray(JSONObject jsonObject) { JSONArray result = new JSONArray(); for (String key : jsonObject.keySet()) { JSONObject entry = new JSONObject(); entry.put("key", key); entry.put("value", jsonObject.get(key)); ...
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class WritingJSONArray { public static void main(String args[]) { //Creating a JSONObject object JSONObject jsonObject = new JSONObject(); //Inserting key-value ...
🌐
Pass4sure
pass4sure.com › blog › creating-a-proper-jsonarray-in-java-with-the-help-of-jsonobject
Creating a Proper JSONArray in Java with the Help of JSONObject – IT Exams Training – Pass4Sure
Create a JSONObject for each user, specifying the name and age. Initialize a JSONArray and use the put() method to insert each user object.
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
Construct a JSONObject from a ResourceBundle. ... JSONException - If any JSONExceptions are detected. ... Constructor to specify an initial capacity of the internal map. Useful for library internal calls where we know, or at least can best guess, how big this JSONObject will be. ... Accumulate values under a key. It is similar to the put method except that if there is already an object stored under the key then a JSONArray is stored under the key to hold all of the accumulated values.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObject.html
JsonObject (Java(TM) EE 7 Specification APIs)
JsonObject value = Json.createObjectBuilder() .add("firstName", "John") .add("lastName", "Smith") .add("age", 25) .add("address", Json.createObjectBuilder() .add("streetAddress", "21 2nd Street") .add("city", "New York") .add("state", "NY") .add("postalCode", "10021")) .add("phoneNumber", Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("type", "home") .add("number", "212 555-1234")) .add(Json.createObjectBuilder() .add("type", "fax") .add("number", "646 555-4567"))) .build(); JsonObject can be written to JSON as follows: JsonWriter writer = ... JsonObject obj = ...; writer.writeObject(obj); JsonObject values can be JsonObject, JsonArray, JsonString, JsonNumber, JsonValue.TRUE, JsonValue.FALSE, JsonValue.NULL.
🌐
Intellipaat
intellipaat.com › home › blog › how to create a correct jsonarray in java using jsonobject?
How to Create a Correct JSONArray in Java Using JSONObject?
May 27, 2025 - Learn how to create a correct JSONArray in Java using JSONObject. Understand how to set up the JSON library, handle nested structures, and format data properly