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
WHENclause is keywordWHENfollowed by a path expression, followed by aTHENclause.The path expression contains a filter condition, which checks for the existence of some data.
-
A
THENorELSEclause is keywordTHENorELSE, respectively, followed by parentheses (()) containing zero or morejson_transformoperations.The operations of a
THENclause are performed if the condition of itsWHENclause is satisfied. The operations of the optionalELSEclause are performed if the condition of noWHENclause is satisfied.Tip: You can use a
THENclause 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_transformCASE, 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/ELSEbranch holds a SQL expression to evaluate, and its value is returned as the result of theCASEexpression. Forjson_transformCASE, eachTHEN/ELSEbranch holds a (parenthesized) sequence ofjson_transformoperations, 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
WHENclause applies to data with fieldQuantitysmaller than5. FieldTotalPriceis calculated with no discount. -
The second
WHENclause applies to data with fieldQuantityat least5but smaller than7. FieldTotalPriceis calculated with a discount of 10%. -
If neither
WHENtest succeeds, theELSEclause calculatesTotalPricewith a discount of 15%.Note that this clause applies also when field
Quantitydoes 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 AI Database SQL Language Reference
Parent topic: Oracle SQL Function JSON_TRANSFORM