JSON Schema in Oracle Database 23ai/26ai
In Oracle database 23ai/26ai a JSON Schema can validate the structure and contents of JSON documents in your database.
oracle 23configurationintermediate
by OracleDba
32 views
In Oracle database 23ai/26ai a JSON Schema can validate the structure and contents of JSON documents in your database.
12345678910
{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
drop table if exists t1 purge;
create table t1 (
id number,
json_data json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}',
constraint t1_pk primary key (id)
);
set long 1000000
column table_name format a10
column column_name format a11
column constraint_name format a15
column json_schema format a40
select table_name,
column_name,
constraint_name,
json_schema
from user_json_schema_columns;
TABLE_NAME COLUMN_NAME CONSTRAINT_NAME JSON_SCHEMA
---------- ----------- --------------- ----------------------------------------
T1 JSON_DATA SYS_C0012374 {"type":"object","properties":{"fruit":{
"type":"string","minLength":1,"maxLength
":10},"quantity":{"type":"number","minim
um":0,"maximum":100}},"required":["fruit
","quantity"]}
SQL>
-- Valid
insert into t1 (id, json_data) values (1, json('{"fruit":"apple","quantity":10}'));
1 row created.
SQL>
-- Valid : Extra weight element.
insert into t1 (id, json_data) values (7, json('{"fruit":"apple","quantity":10,"weight":11}'));
1 row created.
SQL>
-- Missing quantity
insert into t1 (id, json_data) values (2, json('{"fruit":"apple"}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
-- Missing fruit
insert into t1 (id, json_data) values (3, json('{"quantity":10}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
-- Fruit name too long.
insert into t1 (id, json_data) values (4, json('{"fruit":"abcdefghijk","quantity":10}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
-- Fruit name too short.
insert into t1 (id, json_data) values (5, json('{"fruit":"","quantity":10}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
-- Quantity too big.
insert into t1 (id, json_data) values (6, json('{"fruit":"apple","quantity":101}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
-- Quantity too small.
insert into t1 (id, json_data) values (7, json('{"fruit":"apple","quantity":-1}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
drop table if exists t1 purge;
create table t1 (
id number,
json_data json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"],
"additionalProperties" : false
}',
constraint t1_pk primary key (id)
);
insert into t1 (id, json_data) values (7, json('{"fruit":"apple","quantity":10,"weight":11}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
drop table if exists t1 purge;
create table t1 (
id number,
json_data json,
constraint t1_pk primary key (id),
constraint json_data_chk check (json_data is json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}')
);
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"}'));
insert into t1 (id, json_data) values (2, json('{"quantity":10}'));
insert into t1 (id, json_data) values (3, json('{"fruit":"apple","quantity":10}'));
select *
from t1
where json_data is json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}';
ID JSON_DATA
---------- --------------------------------------------------------------------------------
3 {"fruit":"apple","quantity":10}
SQL>123456789101112131415161718192021222324252627282930313233343536373839
drop domain if exists json_schema_domain;
create domain json_schema_domain as json
constraint json_data_chk check (json_schema_domain is json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}');
drop table if exists t1 purge;
create table t1 (
id number,
json_data domain json_schema_domain
);
drop table if exists t1 purge;
drop domain if exists json_schema_domain;
create domain json_schema_domain as json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}';
create table t1 (
id number,
json_data domain json_schema_domain
);1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
drop table if exists t1 purge;
create table t1 (
id number,
json_data json validate '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100},
"expiryDate" : {"extendedType": "date"}},
"required" : ["fruit", "quantity", "expiryDate"]
}',
constraint t1_pk primary key (id)
);
insert into t1 (id, json_data) values (1, json('
{
"fruit" : "apple",
"quantity" : 10,
"expiryDate" : "2023-06-30T09:30:26+0000"
}'));
*
ERROR at line 1:
ORA-40875: JSON schema validation error
SQL>
insert into t1 (id, json_data) values (1, json('
{
"fruit" : "apple",
"quantity" : 10,
"expiryDate" : {"$oracleDate" : "2023-06-30T09:30:26+0000"}
}' extended));
1 row created.
SQL>
drop table if exists t1 purge;
create table t1 (
id number,
json_data json validate cast '{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100},
"expiryDate" : {"extendedType": "date"}},
"required" : ["fruit", "quantity", "expiryDate"]
}',
constraint t1_pk primary key (id)
);
insert into t1 (id, json_data) values (1, json('
{
"fruit" : "apple",
"quantity" : 10,
"expiryDate" : "2023-06-30T09:30:26+0000"
}'));12345678910111213141516171819202122232425
select dbms_json_schema.is_schema_valid('{
"type" : "object",
"properties" : {"fruit" : {"type" : "string",
"minLength" : 1,
"maxLength" : 10},
"quantity" : {"type" : "number",
"minimum" : 0,
"maximum" : 100}},
"required" : ["fruit", "quantity"]
}') as is_valid;
IS_VALID
----------
1
SQL>
select dbms_json_schema.is_schema_valid('banana') as is_valid;
*
ERROR at line 1:
ORA-40441: JSON syntax error
SQL>Please to add comments
No comments yet. Be the first to comment!