Handling Multiple Records Using JSON Arrays

Report views that allow multiple records to be selected by the user frequently pass in arrays of primary key values for selected records, using a JSON array format. JSON arrays can contain either primitive values, such as strings or numbers, or other JSON objects or even nested arrays. The service Java code can access the JSON array as:

JSONArray records = context.getJSONObject("records");


If the data is sent from the report, the JSONArray object will contain individual records in a JSONObject format. You can access the array elements as follows:

for (int i = 0; i < records.length(); i++) {
JSONObject record = records.getJSONObject(i);
// process the record
}


You can also convert the JSONArray into a standard List. Each JSONObject inside the array will be converted to a Map:

List records = fromJSONArray(
context.getJSONArray("records"));
for (int i = 0; i < records.size(); i++) {
Map record = (Map) records.get(i);
// process the record
}
);