Sub topic 8
Changing documents with JSON_TRANSFORM and JSON_MERGEPATCH
Assume you know basic SELECT and UPDATE statements, JSON objects and arrays, and SQL/JSON path expressions. Oracle Database 21c or later is also assumed. The examples use an orders table with a native JSON column named doc.
Use JSON_TRANSFORM for targeted edits
JSON_TRANSFORM changes selected parts of a JSON document. It is useful when you know the path to the value that must change. The original document is not edited directly. The function returns a modified document, which you assign in an UPDATE.
UPDATE orders o
SET o.doc = JSON_TRANSFORM(
o.doc,
SET '$.status' = 'SHIPPED',
REMOVE '$.customer.city'
)
WHERE JSON_VALUE(o.doc, '$.orderId' RETURNING NUMBER) = 1001;This statement performs two operations:
SETchangesstatusto the JSON string"SHIPPED".REMOVEdeletes thecitymember from the nestedcustomerobject.
You can also add a new member or replace an entire object.
UPDATE orders o
SET o.doc = JSON_TRANSFORM(
o.doc,
SET '$.shipping' =
JSON '{"method":"air","cost":15}'
)
WHERE o.id = 1;A path such as $.items[0].qty addresses a particular array element. JSON paths are zero-based for array indexes, so [0] means the first element.
UPDATE orders o
SET o.doc = JSON_TRANSFORM(
o.doc,
SET '$.items[0].qty' = 3
)
WHERE o.id = 1;When a path might be absent, choose the missing-path behavior deliberately. Depending on the operation, Oracle can ignore a missing path, create it, or raise an error. Explicit behavior helps prevent a successful statement from silently leaving the document unchanged.
Use JSON_MERGEPATCH for object-shaped patches
JSON_MERGEPATCH applies a second JSON document, called a patch, to a target document. Object members in the patch replace matching members in the target. New members are added.
SELECT JSON_MERGEPATCH(
'{"status":"NEW",
"customer":{"name":"Ada","city":"London"},
"items":[1,2]}',
'{"status":"PAID",
"customer":{"city":"Paris"}}'
) AS patched_document
FROM dual;The result has status PAID, customer city Paris, and customer name Ada. Members that are not mentioned in the patch remain unchanged.
A member whose patch value is JSON null is removed. This differs from setting a member to a database null value.
SELECT JSON_MERGEPATCH(
'{"status":"NEW","coupon":"SAVE10"}',
'{"coupon":null}'
) AS patched_document
FROM dual;You can apply a patch during an update:
UPDATE orders o
SET o.doc = JSON_MERGEPATCH(
o.doc,
'{"status":"PAID","reviewed":true}'
)
WHERE o.id = 1;Choose the operation that matches the change
JSON_MERGEPATCH is convenient for a client-supplied set of object changes. However, arrays are replaced as whole values. A patch for items must include the complete replacement array. It cannot safely express “change only item 2.”
JSON_TRANSFORM is better for precise edits, array-element updates, removals, and multiple path-based operations.
Exercise
Which function should you use to change only the quantity of the first item in an order, and why?
Show answer
Use JSONTRANSFORM with the path $.items[0].qty. JSONMERGEPATCH replaces an array as a whole, while JSON_TRANSFORM can target one array element.