Parse a JSON array string into a native Deluge List, iterate through it, and extract map properties safely.
Often when calling an external API, the response is not a single JSON object, but an array of objects (e.g., a list of customers). If this data is returned as a raw string, or if you are parsing a webhook payload, you need to convert it into a format Deluge can loop through.
Deluge provides built-in type casting for JSON strings.
If the string begins with a square bracket [, it is a JSON Array. Calling .toMap() on an array will result in an empty map or an execution error. You must explicitly cast it using .toJSONList().
Once converted to a list, you can use the standard for each loop. Inside the loop, each item is treated as a Map, allowing you to use the standard .get("key") syntax to extract values. Always ensure you are checking for null values if the API response structure is unpredictable!
jsonString = "[{\"id\":1, \"name\":\"Apple\"}, {\"id\":2, \"name\":\"Banana\"}]";
// Parse string to list
itemsList = jsonString.toJSONList();
for each item in itemsList
{
// Extract map values
itemId = item.get("id");
itemName = item.get("name");
info "Found item: " + itemName;
}