Skip to content

Collections#

CEL checks membership, searches, and filters lists and maps with built-in operators and macros. Examples on this page use this message:

{
    "id": "ord-123",
    "customer": "alice@example.com",
    "tags": ["express", "gift-wrapped"],
    "items": [
        {"name": "Laptop", "category": "electronics", "price": 999, "quantity": 1},
        {"name": "Cable", "category": "accessories", "price": 29, "quantity": 3}
    ],
    "status": "pending"
}

Membership and access#

in checks whether a value exists in a list.

"express" in order.tags
// result: true (bool)

size() returns the number of elements.

size(order.tags)
// result: 2 (int)
size(order.items)
// result: 2 (int)

Index with [] to access individual elements.

order.tags[0]
// result: "express" (string)

Access nested fields on list elements with dot notation.

order.items[0].name
// result: "Laptop" (string)
order.items[0].price
// result: 999 (int)

Searching with exists()#

exists() tests whether any element satisfies a condition.

order.tags.exists(t, t.startsWith("ex"))
// result: true (bool)
order.items.exists(i, i.price > 500)
// result: true (bool)

Combine multiple checks in the predicate.

order.items.exists(i, i.category == "electronics" && i.price > 100)
// result: true (bool)

Checking all with all()#

all() tests whether every element satisfies a condition.

order.tags.all(t, size(t) > 0)
// result: true (bool)
order.items.all(i, i.quantity > 0)
// result: true (bool)
order.items.all(i, i.price < 10000)
// result: true (bool)

Filtering with filter()#

filter() narrows a list to elements that match a condition.

order.tags.filter(t, t != "express")
// result: ["gift-wrapped"] (list)
order.items.filter(i, i.price > 100)
// result: [{"name": "Laptop", "category": "electronics", "price": 999, "quantity": 1}] (list)
order.items.filter(i, i.category == "electronics")
// result: [{"name": "Laptop", "category": "electronics", "price": 999, "quantity": 1}] (list)

See also#

  • Lists - List literals, indexing, concatenation, and comparison
  • Maps - Map literals, access patterns, and comparison
  • exists() - Existential quantifier reference
  • all() - Universal quantifier reference
  • filter() - List filtering reference
  • At a glance - Every function, macro, and operator