For Each

Discover more about the For Each component and how to use it on the Digibee Integration Platform.

For Each makes a loop inside a JSON structure, processing each element of the array in a subpipeline.

Parameters

Take a look at the configuration parameters of the component. Parameters supported by Double Braces expressions are marked with (DB).

JSON Path Expression Examples

$.[?(@.status == 'EXPIRED')]

The expression above shows how the message received by the component can be filtered: in this example, the array is the root of the object and only the elements whose status attribute is EXPIRED will be processed by For Each.

$.body.*

Obtains all the received message body content.

$.body.products.*

Obtains the content of an array products that's inside the received message body.

Defining the subpipeline to be executed at each iteration

To define the subpipeline to be executed at each iteration, just click on the onProcess icon of For Each.

When clicking on this icon, a subpipeline will be created (or shown, if it already exists). Then build the desired flow according to the execution need of each iteration.

if Session Management components are used to manipulate the data of each array element in the For Each subpipeline and the Parallel Execution option is enabled, it's necessary for the Scoped option from Session Management to be enabled so that each concurrent execution accesses its respective data.

Handling errors in loop

The standard behavior of For Each is to interrupt the execution if some error is found. Errors are atypical situations in the execution of a pipeline that result in a stop. For example, the use of an Assert V2 component causes an error in the pipeline when the assertion condition isn't met. Other error situations occur when components are used with the Fail On Error configuration enabled.

As previously explained, it's possible to define a subpipeline to handle errors. The definition of this subpipeline is made through the onException icon of For Each:

Read the subpipelines article to better understand how it works.

It is not possible to interrupt the entire For Each loop. The interruption can only be done at the current iteration via components where the Fail On Error parameter is enabled in the onProcess and onException subpipelines.

Serious structural error during the onException subpipeline

  • the loop will be interrupted;

  • the error will be thrown to the next component to which For Each is associated;

  • if For Each is in the main pipeline flow, then the pipeline will be interrupted;

  • if For Each is inside a pipeline that has a subpipeline, then the onException subpipeline will be informed in this pipeline input.

Messages flow

Input

For Each accepts any JSON structure that has an array. If the array isn't the root of the object, then a JSON Path expression must be defined to locate and filter the array. If For Each doesn't receive any array, no processing will be executed.

Output

{
    "total": 0,
    "success": 0,
    "failed": 0
}
  • total: total number of processed elements.

  • success: total number of successfully processed elements.

  • failed: total number of elements that couldn't be processed.

To inform that a line has been correctly processed and iterate the value of the "success" field, each execution of the onProcess subpipeline must respond with { "success": true } by the end. That's the only way for the output message to correctly represent the result of the processings.

Last updated