JSON_TRANSFORM in Oracle Database 21c
The JSON_TRANSFORM function was introduced in Oracle database 21c to simplify the modification of JSON data.
oracle 21cconfigurationintermediate
by OracleDba
16 views
The JSON_TRANSFORM function was introduced in Oracle database 21c to simplify the modification of JSON data.
12345678910111213141516171819202122232425262728293031323334353637383940
-- drop table t1 purge;
create table t1 (
id number,
json_data json,
constraint t1_pk primary key (id)
);
insert into t1 (id, json_data) values (1, json('{"fruit":"apple","quantity":10}'));
insert into t1 (id, json_data) values (2, json('{"produce":[{"fruit":"apple","quantity":10},{"fruit":"orange","quantity":15}]}'));
commit;
set linesize 100 pagesize 1000 long 1000000
column data format a60
select id, json_serialize(json_data pretty) as data
from t1;
ID DATA
---------- ------------------------------------------------------------
1 {
"fruit" : "apple",
"quantity" : 10
}
2 {
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
select json_transform(json_data,
set '$.quantity' = 20
returning clob pretty) as data
from t1
where id = 1;
DATA
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
SQL>
select json_transform(json_data,
set '$.updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 1;
DATA
--------------------------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"updated_date" : "2020-12-20T15:35:36.286485Z"
}
SQL>
select json_transform(json_data,
set '$.additional_info' = json('{"colour":"red","size":"large"}')
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"additional_info" :
{
"colour" : "red",
"size" : "large"
}
}
SQL>
select json_transform(json_data,
set '$.additional_info' = '{"colour":"red","size":"large"}' format json
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"additional_info" :
{
"colour" : "red",
"size" : "large"
}
}
SQL>
-- Set quantity to 20 for first item in the produce array.
select json_transform(json_data,
set '$.produce[0].quantity' = 20
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 20
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
-- Add the updated_date element to the first item in the produce array.
select json_transform(json_data,
set '$.produce[0].updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10,
"updated_date" : "2021-01-30T08:10:38.368785Z"
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
-- Add the updated_date element to all items in the produce array.
select json_transform(json_data,
set '$.produce[*].updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10,
"updated_date" : "2021-01-30T08:10:55.828378Z"
},
{
"fruit" : "orange",
"quantity" : 15,
"updated_date" : "2021-01-30T08:10:55.828378Z"
}
]
}
SQL>
select json_transform(json_data,
set '$.updated_date' = systimestamp error on missing
returning clob pretty) as data
from t1
where id = 1;
Error report -
ORA-40762: missing value in JSON_TRANSFORM ()
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
select json_transform(json_data,
insert '$.updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"updated_date" : "2021-01-05T08:44:58.406618Z"
}
SQL>
select json_transform(json_data,
insert '$.fruit' = 'orange'
returning clob pretty) as data
from t1
where id = 1;
Error report -
ORA-40763: existing value in JSON_TRANSFORM ()
SQL>
select json_transform(json_data,
set '$.fruit' = 'orange' error on existing
returning clob pretty) as data
from t1
where id = 1;
ORA-40763: existing value in JSON_TRANSFORM ()
SQL>
-- Added to first position in the array.
select json_transform(json_data,
insert '$.produce[0]' = JSON('{"fruit":"banana","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "banana",
"quantity" : 20
},
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
-- Added to second position in the array.
select json_transform(json_data,
insert '$.produce[1]' = JSON('{"fruit":"banana","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "banana",
"quantity" : 20
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
-- Appended to the end of the array.
select json_transform(json_data,
insert '$.produce[last+1]' = JSON('{"fruit":"banana","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "banana",
"quantity" : 20
}
]
}
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
select json_transform(json_data,
append '$.produce' = JSON('{"fruit":"banana","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "banana",
"quantity" : 20
}
]
}
SQL>
select json_transform(json_data,
insert '$.produce[last+1]' = JSON('{"fruit":"banana","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "banana",
"quantity" : 20
}
]
}
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
-- Remove an element from an object.
select json_transform(json_data,
remove '$.quantity'
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple"
}
SQL>
-- Remove an element from an object in an array.
select json_transform(json_data,
remove '$.produce[0].quantity'
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple"
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
-- Remove an element from the "produce" array.
select json_transform(json_data,
remove '$.produce[1]'
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
}
]
}
SQL>1234567891011121314
select json_transform(json_data,
rename '$.fruit' = 'fruit_name'
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"quantity" : 10,
"fruit_name" : "apple"
}
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344
select json_transform(json_data,
replace '$.quantity' = 20
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 20
}
SQL>
select json_transform(json_data,
replace '$.updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10
}
SQL>
select json_transform(json_data,
replace '$.updated_date' = systimestamp
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10
}
SQL>12345678910111213141516171819202122232425262728293031323334353637383940414243444546
-- Remove everything.
select json_transform(json_data,
keep '$'
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
}
SQL>
-- Remove everything except the "fruit" element.
select json_transform(json_data,
keep '$.fruit'
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple"
}
SQL>
-- Remove everything except the "fruit" and "quantity" elements (remove nothing).
select json_transform(json_data,
keep '$.fruit', '$.quantity'
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10
}
SQL>12345678910111213141516171819
select json_transform(json_data,
set '$.created_date' = systimestamp,
set '$.updated_date' = systimestamp,
rename '$.fruit' = 'fruit_type',
replace '$.quantity' = 20
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"quantity" : 20,
"created_date" : "2020-12-20T17:25:34.539480Z",
"updated_date" : "2020-12-20T17:25:34.539480Z",
"fruit_type" : "apple"
}
SQL>1234567891011121314151617181920212223242526272829
-- Update the data directly in the table.
update t1
set json_data = json_transform(json_data,
set '$.created_date' = systimestamp,
set '$.updated_date' = systimestamp,
rename '$.fruit' = 'fruit_type',
replace '$.quantity' = 20
returning json)
where id = 1;
-- Display the updated data.
select json_serialize(json_data pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"quantity" : 20,
"created_date" : "2020-12-20T17:39:30.811689Z",
"updated_date" : "2020-12-20T17:39:30.811689Z",
"fruit_type" : "apple"
}
SQL>
rollback;123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
set serveroutput on
declare
l_json_in varchar2(32767);
l_json_out varchar2(32767);
begin
l_json_in := '{"fruit":"apple","quantity":10}';
l_json_out := json_transform(l_json_in,
set '$.updated_date' = systimestamp
returning varchar2 pretty);
dbms_output.put_line(l_json_out);
end;
/
Error report -
ORA-06550: line 8, column 36:
PLS-00103: Encountered the symbol "$.updated_date" when expecting one of the following:
. ( ) , * @ % & | = - + < / > at in is mod remainder not null
rem returning with => .. <an exponent (**)> <> or != or ~= >=
<= <> and or default like like2 like4 likec between error ||
multiset member empty submultiset lax strict without pretty
ascii true false absent format allow truncate
The symbol "(" was substituted for "$.updated_date" to continue.
set serveroutput on
declare
l_json_in varchar2(32767);
l_json_out varchar2(32767);
begin
l_json_in := '{"fruit":"apple","quantity":10}';
select json_transform(l_json_in,
set '$.updated_date' = systimestamp
returning varchar2 pretty)
into l_json_out
from dual;
dbms_output.put_line(l_json_out);
end;
/
{
"fruit" : "apple",
"quantity" : 10,
"updated_date" : "2020-12-21T09:44:33.150459Z"
}
PL/SQL procedure successfully completed.
SQL>Please to add comments
No comments yet. Be the first to comment!