You want something like this..

JSONArray jsonArray = new JSONArray(sJSON);
JSONArray jsonPersonData = jsonArray.getJSONArray(1);
for (int i=0; i<jsonPersonData.length(); i++) {
    JSONObject item = jsonPersonData.getJSONObject(i);
    String name = item.getString("name");
    String surname = item.getString("surname");
}
Answer from tomato on Stack Overflow
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
The internal form is an object ... values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object. The constructor can convert a JSON text into a Java object....
🌐
Baeldung
baeldung.com › home › json › introduction to json-java (org.json)
Introduction to JSON-Java (org.json)
June 20, 2025 - However, we should not confuse it with Google’s org.json.simple library. Furthermore, this library can also convert between JSON, XML, HTTP Headers, Cookies, Comma Delimited List, or Text, etc. In this tutorial, we’ll have a look at the following classes: JSONObject – similar to Java’s native Map-like object, which stores unordered key-value pairs · JSONArray – an ordered sequence of values similar to Java’s native Vector implementation
🌐
Javadoc.io
javadoc.io › doc › org.json › json › 20171018 › org › json › class-use › JSONArray.html
Uses of Class org.json.JSONArray
Latest version of org.json:json · https://javadoc.io/doc/org.json/json · Current version 20171018 · https://javadoc.io/doc/org.json/json/20171018 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/org.json/json/20171018/package-list ·
🌐
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 · 中文 – 简体
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray java code examples | Tabnine
Make a prettyprinted JSON text of this JSONArray. Warning: This method assumes that the data structu ... Get the object value associated with an index. ... Get the int value associated with an index. ... Get the optional JSONObject associated with an index.
🌐
GitHub
github.com › stleary › JSON-java › blob › master › src › main › java › org › json › JSONArray.java
JSON-java/src/main/java/org/json/JSONArray.java at master · stleary/JSON-java
A reference implementation of a JSON package in Java. - JSON-java/src/main/java/org/json/JSONArray.java at master · stleary/JSON-java
Author   stleary
🌐
TutorialsPoint
tutorialspoint.com › org_json › org_json_jsonarray.htm
Org.Json - HTTP Class
JSONArray · JSONObject · Number · String · JSONObject.NULL object · package com.tutorialspoint; import org.json.JSONArray; public class JsonDemo { public static void main(String[] args) { JSONArray list = new JSONArray(); list.put("Apple"); list.put("Banana"); list.put("Orange"); list.put("Mango"); list.put("Guava"); System.out.println("JSONArray: "); System.out.println(list); } } JSONArray: ["Apple","Banana","Orange","Mango","Guava"] package com.tutorialspoint; import org.json.JSONArray; public class JsonDemo { public static void main(String[] args) { JSONArray list = new JSONArray(); li
Find elsewhere
🌐
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).
🌐
Maven Repository
mvnrepository.com › artifact › org.json › json
Maven Repository: org.json » json
December 24, 2025 - JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. ... JSON is a light-weight, language independent, data interchange format. See http://www.JSON.org/ The files in this package implement JSON encoders/decoders in Java.
🌐
Javadoc.io
javadoc.io › doc › org.json › json › 20171018 › org › json › JSONArray.html
JSONArray (JSON in Java 20171018 API)
Latest version of org.json:json · https://javadoc.io/doc/org.json/json · Current version 20171018 · https://javadoc.io/doc/org.json/json/20171018 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/org.json/json/20171018/package-list ·
🌐
Miamarti
miamarti.github.io › HorusFramework › javadoc › org › json › simple › JSONArray.html
JSONArray
May 5, 2025 - java.util.ArrayList · org.json.simple.JSONArray · All Implemented Interfaces: java.io.Serializable, java.lang.Cloneable, java.lang.Iterable, java.util.Collection, java.util.List, java.util.RandomAccess, JSONAware, JSONStreamAware · public class JSONArray extends java.util.ArrayList implements java.util.List, JSONAware, JSONStreamAware ·
🌐
GitHub
github.com › stleary › JSON-java › blob › master › src › main › java › org › json › JSONObject.java
JSON-java/src/main/java/org/json/JSONObject.java at master · stleary/JSON-java
throw new JSONException("Failed to convert string to enum: " + value + " for " + enumClass.getName(), e); ... private <T> Collection<T> fromJsonArray(JSONArray jsonArray, Class<?> collectionType, Type elementType) throws JSONException {
Author   stleary
🌐
Oracle
docs.oracle.com › javame › 8.0 › api › json › api › com › oracle › json › JsonArray.html
JsonArray (JSON Documentation)
java.lang.ClassCastException - if the value at the specified position is not assignable to the JsonObject type ... Returns the array value at the specified position in this array. This is a convenience method for (JsonArray)get(index).
Top answer
1 of 1
3

using the following code, I hope it's help you.

//this will be your json object that contains and convert your string to jsonobject
//if you have json object already skip this.
JSONObject yourJSON = new JSONObject(targetString);

//getting the "related" jsonObject
JSONObject related = yourJSON.getJSONObject("related");

//getting the "bought_together" as an jsonArray and do what you want with it.
//you can act with jsonarray like an array
JSONArray bought_together = related.getJSONArray("bought_together");


//now if you run blow code

System.out.print(bought_together.getString(0));

//output is : D202BZX8Z6

-------update according to update the question------ you should change your code like this:

JSONObject object = new JSONObject(sampleText); //sampleText is json that has been split by line

JSONObject related = object.getJSONObject("related");

JSONArray boughtTogether = related.getJSONArray("bought_together");

-------update-------

i think you need to this point (it's not technicality all of they difference)

  • every thing are in {} , they will be JSONObject and the relation is key and value like :

    {"name":"ali"}

    this is a jsonobject and the value of key "name" is ali and we call it like:

    myJsonObject.getString("name");

  • every thing are in [] ,they will be JSONArray and the relation is index and value like :

    ["ali"]

    this is a JsonArray the value of index 0 is ali and we call it
    like:

    myJsonArray.getString(0);

so in your case:

  1. your total object is a JSONObject
  2. the value of "related" key is still a JSONObject
  3. the value of "bought_together" key (which is inside the value of {jsonobject} "related" key) is a JSONArray