Iterate JSON – running commands for every element of a JSON array¶
The command "Iterate JSON" runs its inner commands once for every element of a JSON array. This page is for administrators who process lists from APIs, from LDAP searches or from file lists.
1. At a glance¶
| Purpose | Loop over the elements of a JSON array |
| Typical use | Create a record for every entry of an API answer; process every file of Get Files; every result of an LDAP search |
| Where in the editor | Toolbar menu "Flow" → "Iterate JSON" |
| Needs a user interface | No |
| Related | HTTP Request, Iterate XML, Get Files |
2. Settings¶
The settings are on the tab "Settings". "Display Name", "Description" and "Execution Condition" are on the tab "General" (see Actions). The inner commands are placed in the lane of the command on the canvas.
| Setting | Meaning | Default / Required |
|---|---|---|
| "Json Array" | Expression whose result is a JSON array – for example the response body variable of an HTTP request, or a Modify Value expression that selects an array inside it | Required |
| "Iteration variable" | The variable that receives the current element | Required |
| "Descending" | Runs the elements from the last to the first | Off |
3. Behaviour¶
- If "Json Array" or "Iteration variable" is not set, the command is skipped.
- The expression is evaluated. If the result is not a JSON array at the top level – for example an
object
{ ... }or invalid JSON – the reason is written to the log and the loop runs zero times. An empty array also runs zero times. - For each element, the iteration variable is set and the inner commands run. They work on the same record as the command and share the variables of the action.
- If an inner command ends the action or fails, the loop ends as well.
What the iteration variable contains¶
| Element | Iteration variable |
|---|---|
An object {"id": 7, "name": "Bolt"} |
The object as JSON text |
| An array | The array as JSON text |
A text "Bolt" |
The text without quotes: Bolt |
A number or true/false |
The value as text |
Read values out of an object element with a Modify Value expression and the rule
JObjectJsonPathValue (see Value modification rules).
4. Examples¶
Create an order line for every item of an API answer. The answer of an
HTTP Request in OrderJson looks like this:
{ "order": "A-1043", "items": [ { "sku": "B-8", "qty": 200 }, { "sku": "N-8", "qty": 200 } ] }
- "Json Array": a Modify Value expression on
OrderJsonwith the ruleJObjectJsonPathArray, pathitems - "Iteration variable"
Item - Inside: a command that creates a related order line with
skuandqtyread fromItem
Count elements without a loop. For the number of elements, use the rule JArrayLength instead of
a loop.
5. Limitations¶
- The whole array is read before the loop starts.
- The inner commands cannot change the array while it is being iterated.