Iteration (original) (raw)

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

JSON

[ { "FOR_LOOP_STEP_NAME": { "for": { "value": "LOOP_VARIABLE_NAME", "index": "INDEX_VARIABLE_NAME", "in": "${LIST_EXPRESSION}", "steps": [ { "STEP_NAME_A": ... } ] } } } ]

Replace the following:

Notes:

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

JSON

[ { "FOR_LOOP_STEP_NAME": { "for": { "value": "LOOP_VARIABLE_NAME", "range": "${[BEGIN_EXPRESSION, END_EXPRESSION]}", "steps": [ { "STEP_NAME_A": ... } ] } } } ]

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

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

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.

YAML

JSON