Agent Script Reference: List Variables
A custom variable can be a list of values. A list can hold any supported variable type, such as strings, booleans, numbers, or objects.
When you create a list in the variables block, specify the variable type in brackets. You can leave the list uninitialized, initialize an empty list, or set default values.
To reference an item in a list, use json_path or bracket [] syntax. Lists are zero-indexed, so to get the first item in your list, use [0].
To reference a field in a list of objects, use data.<field_name>. Note that json_path syntax returns the value enclosed in [""] characters, while bracket syntax returns the raw value.
| Syntax | Example | Example Return Value |
|---|---|---|
@variables.<variable_name>[<index>].data.<field_name> | The name of the fourth account in a list of accounts: @variables.all_accounts[3].data.Name | Acme Corp |
json_path(@variables.<variable_name>, "$[<index>].data.<field_name>") | The name of the fourth account in a list of accounts: json_path(@variables.all_accounts, "$[3].data.Name") | ["Acme Corp"] |
This example shows how to use bracket syntax to get the third item in a list of strings.
This example shows:
- How to use
json_pathsyntax to get the value of the Name field from the third account in a list of Account objects. - How to use bracket syntax to get the value of the Name field from the fourth account in a list of Account objects.
You can use these operations on your list variables.
| Operation | Syntax |
|---|---|
| Get the length of a list | len(@variables.myList) |
| Return the maximum of a list of numbers | max(@variables.scores) |
| Return the minimum of a list of numbers | min(@variables.scores) |
| Serialize a variable to a JSON string | to_json(@variables.myList) |
| Use JSONPath syntax to reference a specific value in a list | json_path(@variables.myList, "$[*].name") |
- Pattern: Using Variables Effectively
- Pattern: Using List Variables
- Reference: System Variables
- Reference: Custom Variables
- Flow of Control
- Reference: Utils