CARDINALITY function

The CARDINALITY field function returns the number of repeating fields in a given array.

Syntax

CARDINALITY returns an integer value giving the number of repeating fields in the array specified by field_reference. If field_reference identifies a nonexistent field, zero is returned.

For example, you can use the result to refer to the third to last field:
Body.Invoice."Item"[CARDINALITY(Body.Invoice."Item"[]) - 2].Quantity

In this example, the CARDINALITY function is passed a field reference that ends in []. The meaning of this is count all instances of the Item field. The [] at the end is required; it makes the syntax consistent with other instances where it is necessary to refer to all instances of something. Array indices start at 1, so the array index in the above example refers to the third-from-last instance of the Item field.

Note: If you are using a while loop to process elements of a message, set a variable to the value of the CARDINALITY before entering the loop, to improve performance.
Related concepts
ESQL
Related tasks
Developing ESQL
Converting code page and message encoding
Related reference
Syntax preference
ESQL field functions