Example to find a value in a list using common expression language (CEL)

3.6k views Asked by At

I was following google common expression language specification, can some one guide me if i could do something like this:

I need to write an expression to find if "345" is in the phone_numbers list using google CEL .

json :  {"phone_numbers": ["123","234","345"] }

example :  phone_numbers.exist_one("345"); // this does not works .... 

https://github.com/google/cel-spec/blob/master/doc/langdef.md#standard-definitions

2

There are 2 answers

0
Chetana Dixit On

I got the expression :

phone_numbers.exists_one(r, r=="345")

0
Tristan On

Since you're just testing for the existence of a single value in a list, I would recommend using the in operator:

'345' in phone_numbers

The exists_one macro is pretty useful if '345' can only appear exactly once in the list.