CREATE OR REPLACE TYPE Address_T AS OBJECT (
street VARCHAR2(50),
streetNo NUMBER,
city VARCHAR2(50),
zip VARCHAR2(5)
);
select Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065') from dual
select JSON_OBJECT( 'myKey' : Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065'))
from dual
select JSON_OBJECT(
Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065')
) from dual
select JSON_OBJECT(
Address_T('Oracle Parkway', 500, 'Redwood Shores', null)
) from dual
select JSON_OBJECT(
Address_T('Oracle Parkway', 500, 'Redwood Shores', null) ABSENT ON NULL
) from dual
select JSON_OBJECT(
Address_T('Oracle Parkway', 500, 'Redwood Shores', null) NULL ON NULL
) from dual
select JSON_OBJECT(
Address_T('Oracle Parkway', 500, 'Redwood Shores', null) RETURNING CLOB
) from dual
CREATE OR REPLACE TYPE Person_T AS OBJECT (
first_name VARCHAR2(50),
last_name VARCHAR2(50),
date_of_birth DATE,
address Address_T
);
select JSON_OBJECT(
Person_T ('John', 'Doe', to_date('12-AUG-1989'),
Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065')))
from dual
select JSON_SERIALIZE(
JSON_OBJECT(
Person_T ('John', 'Doe', to_date('12-AUG-1989'),
Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065'))) pretty)
from dual
CREATE Or REPLACE TYPE ThreeD_Point_T AS VARRAY(3) OF number;
select JSON_ARRAY(ThreeD_Point_T(15,-8,19)) from dual
create or replace TYPE address_tab_t as TABLE OF Address_T;
select JSON_ARRAY(
address_tab_t(
Address_T('Oracle Parkway', 500, 'Redwood Shores', '94065'),
Address_T('Twin Dolphin Drive', 10, 'Redwood Shores', '94065')))
from dual
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1989-08-12T00:00:00",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$.ADDRESS' returning Address_T) from dual
create table address_tab (adr address_T)
insert into address_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1989-08-12T00:00:00",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$.ADDRESS' returning Address_T) from dual
select a.adr.city,
a.adr.zip,
a.adr.street
from address_tab a
create table person_tab (per person_T)
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1989-08-12T00:00:00",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.city
from person_tab p
delete from person_tab
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1989-08-12T00:00:00",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T USING CASE-SENSITIVE MAPPING) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.city
from person_tab p
delete from person_tab
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"age": 23,
"playsLottery":"nope"
}', '$' returning Person_T) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.city
from person_tab p
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
"age": 23
}', '$' returning Person_T ERROR ON MISMATCH) from dual
delete from person_tab
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "John",
}', '$' returning Person_T ERROR ON MISMATCH (EXTRA DATA)) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.city
from person_tab p
delete from person_tab
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "Jane",
"an_extra_field" : true,
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1991-03-11",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T ERROR ON MISMATCH (MISSING DATA)) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.city
from person_tab p
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "Jane",
"an_extra_field" : true,
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1991-03-11",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T ERROR ON MISMATCH (EXTRA DATA)) from dual
delete from person_tab
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "Jane",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1991-03-11",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": "500",
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T ERROR ON MISMATCH) from dual
select p.per.first_name,
p.per.address.zip,
p.per.address.streetNo,
p.per.address.city
from person_tab p
insert into person_tab
SELECT JSON_VALUE('
{
"FIRST_NAME": "Jane",
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1991-03-11",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": "500x",
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T ERROR ON MISMATCH) from dual
create table spatial_objects (obj SDO_GEOMETRY)
insert into spatial_objects
SELECT JSON_VALUE('
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [125.6, 10.1]
},
"properties": {
"name": "Dinagat Islands"
}
}' , '$.geometry' returning SDO_GEOMETRY) from dual
select so.obj.SDO_GTYPE,
so.obj.SDO_SRID,
so.obj.SDO_POINT.X, so.obj.SDO_POINT.Y, so.obj.SDO_POINT.Z
from spatial_objects so
select JSON_OBJECT(
SDO_GEOMETRY(2003,NULL,NULL,
SDO_ELEM_INFO_ARRAY(1,1003,4),
SDO_ORDINATE_ARRAY(8,7, 10,9, 8,11)
)
) from dual
select sdo_util.to_json(
SDO_GEOMETRY(2003,NULL,NULL,
SDO_ELEM_INFO_ARRAY(1,1003,4),
SDO_ORDINATE_ARRAY(7,6, 11,8, 7,9)))
from dual
create or replace function anonymize(p1 IN person_t) return person_t is
p2 person_t;
begin
p2 := p1;
p2.address.street := 'unknown';
p2.address.streetno := 0;
return p2;
end;
select JSON_OBJECT(
anonymize(
JSON_VALUE('
{
"FIRST_NAME": "Jane",
"an_extra_field" : true,
"LAST_NAME": "Doe",
"DATE_OF_BIRTH": "1991-03-11",
"ADDRESS": {
"STREET": "Oracle Parkway",
"STREETNO": 500,
"CITY": "Redwood Shores",
"ZIP": "94065"
}
}', '$' returning Person_T )
)) from dual
create or replace function map_to_json (p1 IN person_t) return varchar2 is
v varchar2(100);
begin
v := JSON_OBJECT (p1);
return v;
end;
create or replace function map_to_json (p1 IN person_t) return varchar2 is
v varchar2(100);
begin
execute immediate 'SELECT JSON_OBJECT (:1) from dual' into v using p1;
return v;
end;
select map_to_json((Person_T ('John', 'Doe', to_date('12-AUG-1989'),null)))
from dual
DECLARE
TYPE record_t IS RECORD (first VARCHAR2(20), last VARCHAR2(25) );
name1 record_t;
v varchar2(100);
BEGIN
name1 := record_t('Beda', 'Hammerschmidt');
execute immediate 'SELECT JSON_OBJECT (:1) from dual' into v using name1;
END;
DECLARE
TYPE map_t IS TABLE OF VARCHAR2(30) INDEX BY VARCHAR2(2);
map map_t;
v varchar2(100);
BEGIN
map('ES') := 'Spain';
map('US') := 'United States of America';
map('FR') := 'France';
execute immediate 'SELECT JSON_OBJECT (:1) from dual' into v using map;
END;