Arrays, loops, and filters
Learn how to work with lists of items in your workflows.
You are likely to encounter lists of items (e.g. alerts, devices, etc.) in your workflows. This guide will show you five ways you might work with arrays in your workflows:
We recommend using Action loops
as sparingly as possible.
Use child workflows instead to better encapsulate operations on each item in an array.
For example, in an alert enrichment workflow, you should create two workflows:
Receive alerts
: that receives a list of alertsEnrich alert
: that enriches a single alert
You can then use Enrich alert as a child workflow in Receive alerts to enrich each alert individually. Check out the child workflow loops section for more information.
JSONPath filters
JSONPath allows you to perform advanced filtering on arrays of JSON objects.
Tracecat currently supports the following JSONPath operators:
- Equality operators:
==
,=
,!=
- Comparison operators:
>
,>=
,<
,<=
- Regex match operator:
=~
You can combine multiple criteria using the &
operator.
Properties can only be compared to static values.
Here are some examples of valid filters:
A common use case is to filter data before it is passed to another action.
To do this, use the Reshape
action along with JSONPath filters.
For example:
Action loops
To apply the same action to each item in an array, you must configure at least one Loop expression.
Assign variable
Under the If condition / Loops tab, assign each item in the array to a variable e.g. var.alert
:
Use variable
Call the declared variable in the action inputs:
Filter and loop
You can first filter an array and then run the loop on the filtered array.
Child workflow loops
Add the Execute Child Workflow
action to the workflow.
Copy the workflow ID or alias from the child workflow and paste it into the action inputs.
Similar to action loops, you can configure the child workflow to run once per item in the array.
You can also configure the batch_size
to run the child workflow in batches.
This is useful if you want to limit the number of concurrent child workflow executions at a time.
Inline functions
Tracecat comes with multiple functions that allow you to work with arrays. All functions must be called using the FN
prefix within expressions.
List transformations
- flatten - Flattens nested sequences
- apply - Apply function to each item
List filtering
- filter - Filter items using a lambda function
- unique - Returns unique items
Set operations
- intersect - Returns common elements between sequences
- difference - Returns elements in first sequence not in second
- union - Combines multiple sequences
Advanced operations
- zip - Combine multiple sequences
- iter_product - Generate cartesian product