JSON Data Type Constructor Enhancements in Oracle Database 23ai/26ai
The JSON data type constructor can now accept collections, object types and record types as input.
oracle 23configurationintermediate
by OracleDba
52 views
The JSON data type constructor can now accept collections, object types and record types as input.
12345678910111213141516171819202122232425262728293031323334353637
set serveroutput on
declare
type t_tab is table of varchar2(10) index by pls_integer;
l_tab t_tab;
l_json json;
begin
l_tab := t_tab('banana', 'apple', 'orange');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{"1":"banana","2":"apple","3":"orange"}
PL/SQL procedure successfully completed.
SQL>
set serveroutput on
declare
type t_tab is table of varchar2(10) index by varchar2(10);
l_tab t_tab;
l_json json;
begin
l_tab := t_tab('fruit1' => 'banana',
'fruit2' => 'apple',
'fruit3' => 'orange');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{"fruit1":"banana","fruit2":"apple","fruit3":"orange"}
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617
set serveroutput on
declare
type t_tab is table of varchar2(10);
l_tab t_tab;
l_json json;
begin
l_tab := t_tab('banana', 'apple', 'orange');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{"1":"banana","2":"apple","3":"orange"}
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617
set serveroutput on
declare
type t_tab is varray(5) of varchar2(10);
l_tab t_tab;
l_json json;
begin
l_tab := t_tab('banana', 'apple', 'orange');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
["banana","apple","orange"]
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324
set serveroutput on
declare
type t_rec is record (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
l_rec t_rec;
l_json json;
begin
l_rec := t_rec(id => 1,
val1 => 'banana',
val2 => 'apple');
l_json := json(l_rec);
dbms_output.put_line(json_serialize(l_json));
end;
/
{"ID":1,"VAL1":"banana","VAL2":"apple"}
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617181920212223242526
create or replace type t_obj as object (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
/
set serveroutput on
declare
l_obj t_obj;
l_json json;
begin
l_obj := t_obj(id => 1,
val1 => 'banana',
val2 => 'apple');
l_json := json(l_obj);
dbms_output.put_line(json_serialize(l_json));
end;
/
{"ID":1,"VAL1":"banana","VAL2":"apple"}
PL/SQL procedure successfully completed.
SQL>1234567891011121314151617181920212223242526272829303132333435
set serveroutput on
declare
type t_rec is record (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
type t_tab is table of t_rec index by pls_integer;
l_tab t_tab;
l_json json;
begin
l_tab(1) := t_rec(id => 1,
val1 => 'banana1',
val2 => 'apple1');
l_tab(2) := t_rec(id => 2,
val1 => 'banana2',
val2 => 'apple2');
l_tab(3) := t_rec(id => 3,
val1 => 'banana3',
val2 => 'apple3');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{
"1":{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
"2":{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
"3":{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
}
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627282930313233343536
create or replace type t_obj as object (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
/
set serveroutput on
declare
type t_tab is table of t_obj index by pls_integer;
l_tab t_tab;
l_json json;
begin
l_tab(1) := t_obj(id => 1,
val1 => 'banana1',
val2 => 'apple1');
l_tab(2) := t_obj(id => 2,
val1 => 'banana2',
val2 => 'apple2');
l_tab(3) := t_obj(id => 3,
val1 => 'banana3',
val2 => 'apple3');
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{
"1":{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
"2":{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
"3":{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
}
PL/SQL procedure successfully completed.
SQL>123456789101112131415161718192021222324252627282930313233
set serveroutput on
declare
type t_rec is record (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
type t_tab is table of t_rec index by pls_integer;
l_tab t_tab;
l_json json;
begin
l_tab := t_tab(t_rec(id => 1,
val1 => 'banana1',
val2 => 'apple1'),
t_rec(id => 2,
val1 => 'banana2',
val2 => 'apple2'),
t_rec(id => 3,
val1 => 'banana3',
val2 => 'apple3'));
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{
"1":{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
"2":{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
"3":{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
}
SQL>12345678910111213141516171819202122232425262728293031323334
create or replace type t_obj as object (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
/
set serveroutput on
declare
type t_tab is table of t_obj;
l_tab t_tab;
l_json json;
begin
l_tab := t_tab(t_obj(id => 1,
val1 => 'banana1',
val2 => 'apple1'),
t_obj(id => 2,
val1 => 'banana2',
val2 => 'apple2'),
t_obj(id => 3,
val1 => 'banana3',
val2 => 'apple3'));
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
{
"1":{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
"2":{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
"3":{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
}
SQL>123456789101112131415161718192021222324252627282930313233
set serveroutput on
declare
type t_rec is record (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
type t_tab is varray(5) of t_rec;
l_tab t_tab;
l_json json;
begin
l_tab := t_tab(t_rec(id => 1,
val1 => 'banana1',
val2 => 'apple1'),
t_rec(id => 2,
val1 => 'banana2',
val2 => 'apple2'),
t_rec(id => 3,
val1 => 'banana3',
val2 => 'apple3'));
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
[
{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
]
SQL>1234567891011121314151617181920212223242526272829303132333435
create or replace type t_obj as object (
id number,
val1 varchar2(10),
val2 varchar2(10)
);
/
set serveroutput on
declare
type t_tab is varray(5) of t_obj;
l_tab t_tab;
l_json json;
begin
l_tab := t_tab(t_obj(id => 1,
val1 => 'banana1',
val2 => 'apple1'),
t_obj(id => 2,
val1 => 'banana2',
val2 => 'apple2'),
t_obj(id => 3,
val1 => 'banana3',
val2 => 'apple3'));
l_json := json(l_tab);
dbms_output.put_line(json_serialize(l_json));
end;
/
[
{"ID":1,"VAL1":"banana1","VAL2":"apple1"},
{"ID":2,"VAL1":"banana2","VAL2":"apple2"},
{"ID":3,"VAL1":"banana3","VAL2":"apple3"}
]
SQL>Please to add comments
No comments yet. Be the first to comment!