You can use the Workflows syntax to iterate through a list.
You can use a for
loop to iterate over a sequence of numbers or through a
collection of data, such as a list or map.
You can walk through every item in a list or map using item-based iteration. If you have a specific range of numeric values to iterate through, you can use range-based iteration.
for loops for lists
The following syntax shows an index-based iteration in Workflows:
YAML
- FOR_LOOP_STEP_NAME: for: value: LOOP_VARIABLE_NAME index: INDEX_VARIABLE_NAME in: ${LIST_EXPRESSION} # or simply in: LIST_DEFINITION steps: - STEP_NAME_A: ...
JSON
[ { "FOR_LOOP_STEP_NAME": { "for": { "value": "LOOP_VARIABLE_NAME", "index": "INDEX_VARIABLE_NAME", "in": "${LIST_EXPRESSION}", "steps": [ { "STEP_NAME_A": ... } ] } } } ]
Replace the following:
LOOP_VARIABLE_NAME
: refers to the value of the currently iterated element. The variable name must not be used in assignments or expressions outside of the loop. The same name can be used in multiple loops, as long as they are not nested.INDEX_VARIABLE_NAME
(optional): contains the value to the current offset of the iteration. This always starts at 0 and goes tolen(${LIST_EXPRESSION}) - 1
. The variable name must not be used in assignments or expressions outside of the loop. The same name can be used in multiple loops, as long as they are not nested.LIST_EXPRESSION
: an expression that evaluates into a list or a list definition.
Notes:
for
: required. This is the top-level element indicating the beginning of a for loop.steps
: required. Each iteration thesteps
will be executed.- The 4-space indentation before
value
,index
,in
, andsteps
are required. - The ordering of
value
,index
,in
, andsteps
does not matter. - Do not assign new values in the loop to variables used in the loop expression. This is an undefined behaviour.
- When iterating over maps,
${keys(map)}
can be used to obtain a list of keys.keys()
always returns a sorted list. All loop variables (for example,
INDEX_VARIABLE_NAME
,LOOP_VARIABLE_NAME
, or any variable that is not assigned to outside of the loop) have loop-level scope. These variables are cleared after exiting the loop and accessing them outside of the loop will raise an error when deploying the workflow. For more information, see Variable scope. The following sample demonstrates how any variable created in a loop does not exist outside of that loop:YAML
JSON
Examples
These examples demonstrate the syntax.
List iteration
YAML
JSON
Map iteration
YAML
JSON
Make HTTP requests in a for-in loop
YAML
JSON
Use Google Translate in a for-in loop
YAML
JSON
for loops for number ranges
The following syntax shows a range-based iteration in Workflows:
YAML
- FOR_LOOP_STEP_NAME: for: value: LOOP_VARIABLE_NAME range: ${[BEGIN_EXPRESSION, END_EXPRESSION]} steps: - STEP_NAME_A: ...
JSON
[ { "FOR_LOOP_STEP_NAME": { "for": { "value": "LOOP_VARIABLE_NAME", "range": "${[BEGIN_EXPRESSION, END_EXPRESSION]}", "steps": [ { "STEP_NAME_A": ... } ] } } } ]
for
: Required. This is the top-level element indicating the beginning of a for loop.value
: Required. The variable named inLOOP_VARIABLE_NAME
refers to the value of the currently iterated element. The variable name must not be used in assignments or expressions outside of the loop. The same name can be used in multiple loops, as long as they are not nested.range
: Required. A list of two expressions, specifying the beginning and end of the range, both inclusive.- The expressions must evaluate into incrementable values; that is, integer or double.
- Negative values are allowed. For example,
range: [-10, -1]
will result in 10 iterations. - Floating point values are allowed. Floating point values are not rounded.
For example,
range: [-1.1, -1]
will result in 1 iteration starting at -1.1. - If
END_EXPRESSION
is evaluated to be smaller thanBEGIN_EXPRESSION
, the loop will have 0 iterations.
steps
: Required. Each iteration thesteps
will be executed.Each iteration increments the loop counter by 1, or when doubles are provided by 1.0. For example,
range: [1.1, 2.8]
causes the loop to have two iterations.The 4-space indentation before
value
,range
, andsteps
are required.The ordering of
value
,range
, andsteps
does not matter.All loop variables (for example,
LOOP_VARIABLE_NAME
or any variable that is not assigned to outside of the loop) have loop-level scope. These variables are cleared after exiting the loop and accessing them outside of the loop will raise an error when deploying the workflow. For more information, see Variable scope.
Examples
These examples demonstrate the syntax.
Basic for-range
YAML
JSON
for-range to make HTTP requests
YAML
JSON
Jump within a loop
Only jumping between named steps belonging to the same for loop is allowed. Jumping in or out of a for loop, or between two different for loops, is not allowed.
YAML
- FOR_LOOP_STEP_NAME: for: value: LOOP_VARIABLE_NAME in: ${LIST_EXPRESSION_A} # or simply in: LIST_DEFINITION steps: - STEP_NAME_A: next: STEP_NAME_C - STEP_NAME_B: next: STEP_NAME_C - STEP_NAME_C: ...
JSON
[ { "FOR_LOOP_STEP_NAME": { "for": { "value": "LOOP_VARIABLE_NAME", "in": "${LIST_EXPRESSION_A}", "steps": [ { "STEP_NAME_A": { "next": "STEP_NAME_C" } }, { "STEP_NAME_B": { "next": "STEP_NAME_C" } }, { "STEP_NAME_C": ... } ] } } } ]
Use break/continue in a loop
To change the flow of a for loop, you can use next: break
or next: continue
.
Note that break
and continue
are reserved jump targets defined implicitly
within a loop. Named steps called break
or continue
within a for loop are
not allowed; however, they are allowed outside of a for loop.
YAML
- FOR_LOOP_STEP_NAME_A: for: value: LOOP_VARIABLE_NAME_A in: ${LIST_EXPRESSION_A} # or simply in: LIST_DEFINITION steps: - STEP_NAME_A: next: continue - FOR_LOOP_STEP_NAME_B: for: value: LOOP_VARIABLE_NAME_B range: ${[BEGIN_EXPRESSION, END_EXPRESSION]} steps: - STEP_NAME_B: next: break
JSON
[ { "FOR_LOOP_STEP_NAME_A": { "for": { "value": "LOOP_VARIABLE_NAME_A", "in": "${LIST_EXPRESSION_A}", "steps": [ { "STEP_NAME_A": { "next": "continue" } } ] } } }, { "FOR_LOOP_STEP_NAME_B": { "for": { "value": "LOOP_VARIABLE_NAME_B", "range": "${[BEGIN_EXPRESSION, END_EXPRESSION]}", "steps": [ { "STEP_NAME_B": { "next": "break" } } ] } } } ]
Iterate through a list
This sample shows how you can use a combination of conditional jumps, variables,
and the len()
function to iterate through a list.