JSON_TRANSFORM Enhancements in Oracle Database 23ai/26ai
In Oracle 23ai/26ai the JSON_TRANSFORM function has lots of new operations, conditional control and richer path support.
oracle 23configurationintermediate
by OracleDba
16 views
In Oracle 23ai/26ai the JSON_TRANSFORM function has lots of new operations, conditional control and richer path support.
1234567891011121314151617181920212223242526272829303132333435363738394041
drop table if exists 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}')),
(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>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
select json_transform(json_data,
prepend '$.produce' = 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>
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>1234567891011121314151617181920
select json_transform(json_data,
copy '$.produce' = json('{"fruit":"lime","quantity":20}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "lime",
"quantity" : 20
}
]
}
SQL>123456789101112131415161718192021222324252627282930313233343536373839
select json_transform(json_data,
prepend '$.produce' = json('{"fruit":"orange","quantity":15}'),
minus '$.produce' = json('{"fruit":"apple","quantity":10}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
select json_transform(json_data,
minus '$.produce' = path '$temp[*]'
passing json('[{"fruit":"apple","quantity":10},{"fruit":"orange","quantity":15}]') as "temp"
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
]
}
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
select json_transform(json_data,
prepend '$.produce' = json('{"fruit":"apple","quantity":10}'),
intersect '$.produce' = json('{"fruit":"apple","quantity":10}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
}
]
}
SQL>
select json_transform(json_data,
intersect '$.produce' = path '$temp[*]'
passing json('[{"fruit":"apple","quantity":10},{"fruit":"orange","quantity":15}]') as "temp"
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
select json_transform(json_data,
prepend '$.produce' = json('{"fruit":"apple","quantity":10}'),
union '$.produce' = json('{"fruit":"lime","quantity":12}')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "lime",
"quantity" : 12
}
]
}
SQL>
select json_transform(json_data,
union '$.produce' = path '$temp[*]'
passing json('[{"fruit":"lime","quantity":12},{"fruit":"lemon","quantity":20}]') as "temp"
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "lemon",
"quantity" : 20
},
{
"fruit" : "lime",
"quantity" : 12
}
]
}
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
select json_transform(json_data,
prepend '$.produce' = JSON('{"fruit":"kiwi","quantity":30}'),
sort '$.produce'
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "kiwi",
"quantity" : 30
},
{
"fruit" : "orange",
"quantity" : 15
}
]
}
SQL>
select json_transform(json_data,
prepend '$.produce' = JSON('{"fruit":"kiwi","quantity":30}'),
sort '$.produce' desc
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "kiwi",
"quantity" : 30
},
{
"fruit" : "apple",
"quantity" : 10
}
]
}
SQL>
select json_transform(json_data,
prepend '$.produce' = JSON('{"fruit":"kiwi","quantity":30}'),
sort '$.produce' order by '$.quantity' desc
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "kiwi",
"quantity" : 30
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "apple",
"quantity" : 10
}
]
}
SQL>12345678910111213141516171819202122232425262728293031323334
select json_transform(json_data,
merge '$' = json('{"weight":20, "lifespan":5}')
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"weight" : 20,
"lifespan" : 5
}
select json_transform(json_data,
merge '$' = path '$temp[*]'
passing json('[{"weight":20}, {"lifespan":5}, {"food miles":1000}]') as "temp"
returning clob pretty) as data
from t1
where id = 1;
DATA
------------------------------------------------------------
{
"fruit" : "apple",
"quantity" : 10,
"weight" : 20,
"lifespan" : 5,
"food miles" : 1000
}
SQL>1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
select json_transform(json_data,
nested path '$.produce[*]'
(set '@.quantity' = path '@.quantity + 5',
insert '@.weight' = 20)
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 15,
"weight" : 20
},
{
"fruit" : "orange",
"quantity" : 20,
"weight" : 20
}
]
}
SQL>
select json_transform(json_data,
set '$temp' = json('[{"fruit":"lime","quantity":12},{"fruit":"lemon","quantity":24},{"fruit":"apple","quantity":10}]'),
union '$.produce' = path '$temp[*]'
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
},
{
"fruit" : "lemon",
"quantity" : 24
},
{
"fruit" : "lime",
"quantity" : 12
}
]
}
SQL>12345678910111213141516171819202122232425262728293031323334353637383940
select json_transform(json_data,
nested path '$.produce[*]' (
case
when '@.fruit == "apple"' then (
insert '@.weight' = 10,
insert '@.lifespan' = 5
)
when '@.fruit == "orange"' then (
insert '@.weight' = 12
)
else (
insert '@.weight' = null
)
end
)
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10,
"weight" : 10,
"lifespan" : 5
},
{
"fruit" : "orange",
"quantity" : 15,
"weight" : 12
}
]
}
SQL>123456789101112131415161718192021222324252627282930
select json_transform(json_data,
nested path '$.produce[*]'
(set '@.weight' = 10,
set '@.total_weight' = path '@.quantity * @.weight')
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10,
"weight" : 10,
"total_weight" : 100
},
{
"fruit" : "orange",
"quantity" : 15,
"weight" : 10,
"total_weight" : 150
}
]
}
SQL>1234567891011121314151617181920212223242526272829303132
select json_transform(json_data,
set '$.count_entries' = path '@.produce[*].count()',
set '$.sum_quantity' = path '@.produce[*].quantity.sum()',
set '$.avg_quantity' = path '@.produce[*].quantity.avg()',
set '$.min_quantity' = path '@.produce[*].quantity.min()',
set '$.max_quantity' = path '@.produce[*].quantity.max()'
returning clob pretty) as data
from t1
where id = 2;
DATA
------------------------------------------------------------
{
"produce" :
[
{
"fruit" : "apple",
"quantity" : 10
},
{
"fruit" : "orange",
"quantity" : 15
}
],
"count_entries" : 2,
"sum_quantity" : 25,
"avg_quantity" : 12.5,
"min_quantity" : 15,
"max_quantity" : 15
}
SQL>Please to add comments
No comments yet. Be the first to comment!