13.5 JSON_TRANSFORM Operator CASE
JSON_TRANSFORM
operator CASE
conditionally
performs a sequence of operations. It is a control operation: it conditionally
applies other json_transform
operations, which in turn can modify
data.
The syntax of a CASE
operation is operator
CASE
followed by one or more WHEN
clauses,
followed optionally by an ELSE
clause, followed by keyword
END
.
-
A
WHEN
clause is keywordWHEN
followed by a path expression, followed by aTHEN
clause.The path expression contains a filter condition, which checks for the existence of some data.
-
A
THEN
orELSE
clause is keywordTHEN
orELSE
, respectively, followed by parentheses (()
) containing zero or morejson_transform
operations.The operations of a
THEN
clause are performed if the condition of itsWHEN
clause is satisfied. The operations of the optionalELSE
clause are performed if the condition of noWHEN
clause is satisfied.Tip: You can use a
THEN
clause with zero operations to conditionally prevent the use of any subsequent clauses.
The conditional path expressions of the WHEN
clauses are
tested in order, until one succeeds (those that follow are not tested). The
THEN
operations for the successful WHEN
test are
then performed, in order.
If none of the WHEN
tests succeeds then the operations of
the optional ELSE
clause are performed, in order.
Note:
A SQL
CASE
expression differs from a
json_transform
CASE
operation, in these respects:
-
For SQL
CASE
, the predicate tested is a SQL comparison. Forjson_transform
CASE
, the predicate is a path expression that checks for the existence of some data. (The check is essentially done usingjson_exists
.) -
For SQL
CASE
, eachTHEN
/ELSE
branch holds a SQL expression to evaluate, and its value is returned as the result of theCASE
expression. Forjson_transform
CASE
, eachTHEN
/ELSE
branch holds a (parenthesized) sequence ofjson_transform
operations, which are performed, in order.
Example 13-11 JSON_TRANSFORM: Controlling Modifications with CASE and SET
This example uses operators CASE
and
SET
to set field TotalPrice
conditionally,
creating it if it doesn't exist. When applied to the data, each
WHEN
test is tried in turn, until one succeeds. The
SET
operation corresponding to that successful test is then
performed. If no WHEN
test succeeds then the ELSE
clause is used.
-
The first
WHEN
clause applies to data with fieldQuantity
smaller than5
. FieldTotalPrice
is calculated with no discount. -
The second
WHEN
clause applies to data with fieldQuantity
at least5
but smaller than7
. FieldTotalPrice
is calculated with a discount of 10%. -
If neither
WHEN
test succeeds, theELSE
clause calculatesTotalPrice
with a discount of 15%.Note that this clause applies also when field
Quantity
does not exist or is a non-numeric JSON value that does not compare less than7
.
json_transform(
data,
NESTED PATH '$.LineItems[*]'
( CASE WHEN '@?(@.Quantity < 5)' THEN
( -- No discount
SET '@.TotalPrice' = PATH '@.Quantity * @.Part.UnitPrice' )
WHEN '@?(@.Quantity < 7)' THEN
( -- 10% discount
SET '@.TotalPrice' = PATH '@.Quantity * @.Part.UnitPrice * 0.9' )
ELSE
( -- 15% discount
SET '@.TotalPrice' = PATH '@.Quantity * @.Part.UnitPrice * 0.85' )
END ))
No handlers are allowed for operator CASE
. (But
operators used in the CASE
branches can use any handlers they're
allowed.)
See Also:
CASE Expressionsin Oracle Database SQL Language Reference
Parent topic: Oracle SQL Function JSON_TRANSFORM