Skip to content

Maps#

Maps in CEL are unordered collections of key-value pairs. JSON objects are represented as maps with string keys.

Literals#

Create maps using curly braces with key-value pairs.

{"a": 1, "b": 2}
// result: {"a": 1, "b": 2} (map)
{}
// result: {} (map)

Keys can be strings, integers, unsigned integers, or booleans. Both keys and values can be heterogeneous.

{1: "one", 2: "two"}
// result: {1: "one", 2: "two"} (map)
{"a": 1, 2: "two", true: 3}
// result: {"a": 1, 2: "two", true: 3} (map)

Maps can be nested.

{"nested": {"a": 1}}
// result: {"nested": {"a": 1}} (map)

Size#

size() returns the number of key-value pairs. Both function and method syntax are supported.

size({"a": 1, "b": 2, "c": 3})
// result: 3 (int)
{"a": 1, "b": 2}.size()
// result: 2 (int)

Access#

Access values using bracket notation with the key.

{"a": 1, "b": 2}["a"]
// result: 1 (int)

String keys also support dot notation.

{"a": 1, "b": 2}.a
// result: 1 (int)

Use bracket notation for keys that conflict with CEL reserved words.

{"in": 1}["in"]
// result: 1 (int)

Accessing a missing key produces a runtime error.

{"a": 1}["missing"]
// error: no such key: missing

Membership#

The in operator checks if a key exists in a map.

"a" in {"a": 1, "b": 2}
// result: true (bool)
"c" in {"a": 1, "b": 2}
// result: false (bool)

Comparison#

Maps support equality comparison. Two maps are equal if they have the same keys with the same values.

{"a": 1, "b": 2} == {"a": 1, "b": 2}
// result: true (bool)
{"a": 1} == {"a": 2}
// result: false (bool)

See also#