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 provides various accessor methods to access the values in an array. The following example shows how to obtain the home phone number "212 555-1234" from the array built in the previous example: JsonObject home = array.getJsonObject(0); String number = home.getString("number");
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
A JSONObject value. ... Get the long value associated with an index. ... JSONException - If the key is not found or if the value cannot be converted to a number. public String getString(int index) throws JSONException · Get the string associated with an index. ... A string value. ... JSONException - If there is no string value for the index. ... Determine if the value is null. ... Make a string from the contents of this JSONArray.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-within-jsonobject-in-java
How can we add a JSONArray within JSONObject in Java?
public JSONObject put(java.lang.String key, java.util.Collection<?> value) throws JSONException · import org.json.*; public class AddJSONArrayTest { public static void main(String[] args) throws JSONException { JSONArray array = new JSONArray(); array.put("INDIA"); array.put("AUSTRALIA"); array.put("ENGLAND"); JSONObject obj = new JSONObject(); obj.put("COUNTRIES", array); System.out.println(obj); } }
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-to-jsonobject-in-java
How can we add a JSONArray to JSONObject in Java?
July 4, 2020 - import org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest { public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("Raja"); list.add("Jai"); list.add("Adithya"); JSONArray array = new JSONArray(); for(int i = 0; i < list.size(); i++) { array.put(list.get(i)); } JSONObject obj = new JSONObject(); try { obj.put("Employee Names:", array); } catch(JSONException e) { e.printStackTrace(); } System.out.println(obj.toString()); } }
🌐
SourceForge
json-lib.sourceforge.net › apidocs › net › sf › json › JSONArray.html
JSONArray (Overview (json-lib jdk 1.3 API))
A JSONObject value. ... Get the long value associated with an index. ... JSONException - If the key is not found or if the value cannot be converted to a number. ... Get the string associated with an index. ... A string value. ... JSONException - If there is no value for the index. ... Returns true if this object is a JSONArray, false otherwise.
🌐
Coderanch
coderanch.com › t › 706235 › java › Identifying-JSONObject-JSONArray
Identifying JSONObject or JSONArray (Java in General forum at Coderanch)
February 12, 2019 - I figured it out (I think). I used a try/catch on the parser then caught a ParserException and processed it as an JSONArray instead an JSONObject.
Find elsewhere
🌐
Processing
processing.org › reference › jsonarray
JSONArray / Reference / Processing.org
String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" }; String[] names = { "Goat", "Leopard", "Zebra" }; JSONArray values; void setup() { values = new JSONArray(); for (int i = 0; i < species.length; i++) { JSONObject animal = new JSONObject(); animal.setInt("id", i); animal.setString("species", species[i]); animal.setString("name", names[i]); values.setJSONObject(i, animal); } saveJSONArray(values, "data/new.json"); } // Sketch saves the following to a file called "new.json": // [ // { // "id": 0, // "species": "Capra hircus", // "name": "Goat" // }, // { // "id": 1, // "species": "Panthera pardus", // "name": "Leopard" // }, // { // "id": 2, // "species": "Equus zebra", // "name": "Zebra" // } // ] JSONArray() getString()Gets the String value associated with an index ·
🌐
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 - Beginner's Guide * */ public class CrunchifyParseJsonObjectAndArray { public static void main(String[] args) { // JSON object string String jsonObjectString = "{\"name\":\"Crunchify\",\"year\":2023,\"city\":\"New York\"}"; // Parse the JSON object string into a JSONObject JSONObject jsonObject = new JSONObject(jsonObjectString); // Extract values from the JSON object String name = jsonObject.getString("name"); int year = jsonObject.getInt("year"); String city = jsonObject.getString("city"); // Print the values System.out.println("Name: " + name); System.out.println("Year: " + year); System.out
🌐
Baeldung
baeldung.com › home › json › get a value by key in a jsonarray
Get a Value by Key in a JSONArray | Baeldung
January 8, 2024 - A JSON message is usually comprised of JSON objects and arrays which may be nested inside one another. A JSONArray object is enclosed within square brackets [ ] whereas a JSONObject is enclosed within curly braces {}. For instance, let’s consider ...
🌐
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
🌐
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 ... – a standard exception thrown by this library · A JSONObject is an unordered collection of key and value pairs, resembling Java’s native Map implementations....
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
A JSONObject is an unordered collection of name/value pairs. Its external form is a string wrapped in curly braces with colons between the names and values, and commas between the values and names. The internal form is an object having get and opt methods for accessing the values by name, and ...
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray java code examples | Tabnine
Construct a JSONArray from a JSONTokener. ... Get the JSONObject associated with an index.