Skip to content

Transforming data#

CEL expressions return any type, not just booleans. Build maps, transform collections, and compute values. Examples on this page use this message:

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

Building maps#

Construct a new map to reshape or summarize data.

{"tags": order.tags, "is_adult": order.customer_age >= 18}
// result: {"is_adult": true, "tags": ["express", "gift-wrapped"]} (map)
{"customer": order.customer, "item_count": size(order.items)}
// result: {"customer": "alice@example.com", "item_count": 2} (map)

Transforming with map()#

map() transforms each element of a list into a new value.

order.tags.map(t, t + ":enabled")
// result: ["express:enabled", "gift-wrapped:enabled"] (list)
order.items.map(i, i.name)
// result: ["Laptop", "Cable"] (list)

Transform each element into a map.

order.tags.map(t, {"tag": t, "premium": t == "express"})
// result: [{"premium": true, "tag": "express"}, {"premium": false, "tag": "gift-wrapped"}] (list)
order.items.map(i, {"name": i.name, "total": i.price * i.quantity})
// result: [{"name": "Laptop", "total": 999}, {"name": "Cable", "total": 87}] (list)

Filter and transform#

The three-argument form of map() filters and transforms in one step. The second argument is the filter condition, the third is the transform.

order.tags.map(t, t != "express", t + ":applied")
// result: ["gift-wrapped:applied"] (list)
order.items.map(i, i.price > 100, i.name)
// result: ["Laptop"] (list)

Combining techniques#

Build structured output that filters, transforms, and reshapes.

{"id": order.id, "electronics": order.items.filter(i, i.category == "electronics").map(i, i.name)}
// result: {"electronics": ["Laptop"], "id": "ord-123"} (map)
{"customer": order.customer, "express": "express" in order.tags, "pending": order.status == "pending"}
// result: {"customer": "alice@example.com", "express": true, "pending": true} (map)

See also#

  • Maps - Map literals, access patterns, and comparison
  • map() - Two-argument and three-argument forms, map key iteration
  • filter() - List filtering reference
  • Arithmetic operators - Numeric operations and precedence
  • At a glance - Every function, macro, and operator