Working with Arrays
AMPS supports filters that operate on arrays in messages. There are two simple principles behind how AMPS treats arrays:
Binary operators that yield
true
orfalse
(for example,=
,<
,LIKE
) are array aware, as is theIN
operator. These operators work on arrays as a whole, and evaluate every element in the array.Arithmetic operators, functions, user-defined functions and other scalar operators, are not array aware, and use the first element in the array.
With these simple principles, you can predict how AMPS will evaluate an expression that uses an array. For any operator, an empty array evaluates to NULL
.
Let's look at some examples. For the purposes of this section, we will consider the following JSON document:
While these arrays are presented using JSON format for simplicity, the same principles apply to arrays in other message formats.
Here are some examples of ways to use an array in an AMPS filter:
Determining if any element in an array meets a criteria
To determine this, you provide the identifier for the array, and use a comparison operator.
Filter | Evaluates as |
---|---|
| TRUE, |
| TRUE, |
| TRUE, |
| FALSE, |
| TRUE, a member of |
| TRUE, a member of |
| FALSE, no member of |
Determine whether a specific value is at a specific position
To determine this, use the subscript operator []
on the XPath identifier to specify the position, and use the equality operator to check the value at that position.
Filter | Evaluates as |
---|---|
| TRUE, first element of |
| TRUE, fourth element of |
| TRUE, second element of |
| TRUE, second element of |
Determine whether any value in one array is present in another array
Filter | Evaluates as |
---|---|
| TRUE, a value in |
| TRUE, a value in |
Determine whether an array contains one of a set of values
Filter | Evaluates as |
---|---|
| TRUE, |
| TRUE, a member of |
| TRUE, a member of |
These patterns and principles hold regardless of the original representation of the array in a document.
When creating an expression that uses a field in a compound value, keep in mind that AMPS represents compound values as described in the section on Compound Data Types in AMPS.
Last updated