You need to create a new jsonObj reference with every iteration of the loop:

for (int j = 0; j < X.size(); j++)
 {
  zBean aBean = (zBean)X.get(j);
  jsonObj = new JSONObject();
//^^^^^^^^^^^^^^^^^^^^^^^^^^^ add this line
  jsonObj.put(ID,newInteger(aBean.getId()));
  jsonObj.put(NAME,aBean.getName());
  jsonArray.add(jsonObj);
 }

Otherwise you are updating the same instance over and over again, and adding a reference to the same object many times to the array. Since they are all the same reference, a change to one of them affects all of them in the array.

Answer from mellamokb on Stack Overflow
🌐
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()); } } {"Employee Names:":["Raja","Jai","Adithya"]} raja ·
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
JsonArray instances are list objects that provide read-only access to the values in the JSON array. Any attempt to modify the list, whether directly or using its collection views, results in an UnsupportedOperationException. ... add, add, addAll, addAll, clear, contains, containsAll, equals, ...
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.put java code examples | Tabnine
private JSONArray convertListToJsonArray(Object value) throws InvocationTargetException, IllegalAccessException { JSONArray array = new JSONArray(); List<Object> list = (List<Object>) value; for(Object obj : list) { // Send null, if this is an array of arrays we are screwed array.put(obj != null ?
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - //Creating a JSONObject object ... class. jsonObject.put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class....
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
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
🌐
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONArray.html
JSONArray
Append a boolean value. This increases the array's length by one. ... Put or replace a String value. If the index is greater than the length of the JSONArray, then null elements will be added as necessary to pad it out.
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-within-jsonobject-in-java
How can we add a JSONArray within JSONObject in Java?
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); } }
🌐
Tabnine
tabnine.com › home page › code › java › com.google.gson.jsonarray
com.google.gson.JsonArray.add java code examples | Tabnine
@Override public void userDelete(String[] userids) throws WxErrorException { String url = "https://qyapi.weixin.qq.com/cgi-bin/user/batchdelete"; JsonObject jsonObject = new JsonObject(); JsonArray jsonArray = new JsonArray(); for (int i = 0; i < userids.length; i++) { jsonArray.add(new JsonPrimitive(userids[i])); } jsonObject.add("useridlist", jsonArray); post(url, jsonObject.toString()); } ... @Override public PersistedData create(boolean... values) { JsonArray array = new JsonArray(); for (boolean val : values) { array.add(new JsonPrimitive(val)); } return new GsonPersistedData(array); }
🌐
Stack Overflow
stackoverflow.com › questions › 34854560 › java-jsonobject-adding-a-new-element-in-arrays-jsonarray
JAVA JSONObject adding a new element in Arrays (JSONArray) - Stack Overflow
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONObject; import org.junit.Before; import org.junit.Test; public class JettisonJSONOperations { JSONObject jsonObject = new JSONObject(); @Before public void preCreateJsonObject (){ try { jsonObject.put("groupName","administrators"); JSONArray members = new JSONArray(); members.put("edward"); members.put("richard"); members.put("john"); jsonObject.put("members", members); } catch (JSONException e) { e.printStackTrace(); } System.out.println(jsonObject.toString()); } @Test public void addingElementToJSONArray (){ try { jsonObject.append("members", "batman"); } catch (JSONException e) { e.printStackTrace(); } System.out.println(jsonObject.toString()); } }
🌐
Processing
processing.org › reference › JSONArray_append_.html
JSONArray - append() / Reference / Processing.org
January 1, 2021 - Appends a new value to the JSONArray, increasing the array's length by one. New values may be of the following types: int, float, String, boolean, JSONObject,…
🌐
Couchbase
docs.couchbase.com › sdk-api › couchbase-java-client › com › couchbase › client › java › json › JsonArray.html
JsonArray (Couchbase Java SDK 3.11.1 API)
Static method to create a JsonArray from a JSON String. Not to be confused with from(Object...) from(aString)} which will populate a new array with the string. The string is expected to be a valid JSON array representation (eg.
🌐
Stack Overflow
stackoverflow.com › questions › 50039189 › how-to-add-object-to-jsonarray
java - How to add object to JSONArray - Stack Overflow
April 26, 2018 - public class Test { public static void main(String[] args) { Mypojo mypojo = new Mypojo(); Gson gson = new Gson(); JSONArray records = new JSONArray(); for (int i = 0; i < 1; i++) { if (5 > 0) { mypojo.setPoAccount("050017"); mypojo.setPoAmount("12"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } mypojo.setPoAccount("050016"); mypojo.setPoAmount("800"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } System.out.println(records); } }
🌐
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: JSONArray ja = new JSONArray("[true, \"lorem ipsum\", 215]"); This constructor may throw a JSONException if the source String isn’t ...