case()
Evaluates an expression with branching based on multiple conditions.
Syntax
case(CONDITION_1, EXPR_1[, CONDITION_2, EXPR2, ...], DEFAULT_EXPR)
Required Parameter
CONDITION_1, EXPR_1[, CONDITION_2, EXPR2, ...]
- Specify the conditional statement in pairs with the evaluation expression (EXPR_N) to be executed when it is
true
or notnull
. DEFAULT_EXPR
- Specify the expression to be performed when none of the evaluation conditions are met.
Usage
-
Evaluate a score over 90 as A, 80 as B, 70 as C, 60 as D, and others as F.
json "[ {'Name': 'Alice', 'Score': 98}, {'Name': 'Bob', 'Score': 65}, {'Name': 'Clark', 'Score': 40} ]" | eval Grade=case( Score > 90, "A", Score > 80, "B", Score > 70, "C", Score > 60, "D", "F") | order Name, Grade, Score
-
When the string length of the
str
field is greater than 9, cut it into 9 characters and apply an ellipsis.json "[ {'str': 'Somewhere over the rainbow'}, {'str': 'Wonderful'} ]" | eval truncated=case(len(str) > 10, concat(left(str, 10), "…"), str)