Drop the table if it already exists
drop table JSON_EXAMPLE1
ORA-00942: table or view does not existMore Details: https://docs.oracle.com/error-help/db/ora-00942
Create a table to contain JSON data.
create table JSON_EXAMPLE1(
PKEY NUMBER(2),
JSON_COLUMN VARCHAR2(64)
)
Table created.
Insert Data
insert into JSON_EXAMPLE1 values (1, '{ "MyKey1" : "Value1", "MyKey2" : "Value2" }')
1 row(s) inserted.
Commit
commit
Statement processed.
REM Attempt to generate an JSON object with a Key called "MyObject" whose value is the JSON document. Note the value is a JSON String, not a JSON Object. One way of getting a JSON Object is to add an IS JSON constraint to the table.
Attempt to generate an JSON object with a Key called "MyObject" whose value is the JSON document. Note the value is a JSON String, not a JSON Object. One way of getting a JSON Object is to add an IS JSON constraint to the table.
Attempt to use Simple Syntax for JSON on the column containing the JSON documents
select PKEY, ex.JSON_COLUMN.MyKey1, ex.JSON_COLUMN.MyKey2
from JSON_EXAMPLE1 ex
ORA-00904: "EX"."JSON_COLUMN"."MYKEY2": invalid identifierMore Details: https://docs.oracle.com/error-help/db/ora-00904
REM In 18c we can force the database to treat VARCHAR2, CLOB and BLOB columns as JSON using TREAT( String as JSON)
In 18c we can force the database to treat VARCHAR2, CLOB and BLOB columns as JSON using TREAT( String as JSON)
with MY_JSON_TABLE as
(
select PKEY, TREAT(JSON_COLUMN as JSON) JSON_COLUMN
from JSON_EXAMPLE1
)
select PKEY, j.JSON_COLUMN.MyKey1, j.JSON_COLUMN.MyKey2
from MY_JSON_TABLE j
PKEY | MYKEY1 | MYKEY2 | 1 | Value1 | Value2 |
---|
create or replace view JSON_ENABLED_VIEW
as
select TREAT(JSON_COLUMN as JSON) JSON_COLUMN
from JSON_EXAMPLE1
View created.
select j.JSON_COLUMN.MyKey1,j.JSON_COLUMN.MyKey2
from JSON_ENABLED_VIEW j
MYKEY1 | MYKEY2 | Value1 | Value2 |
---|
select JSON_OBJECT('MyObject' value JSON_COLUMN) JSON
from JSON_EXAMPLE1
JSON | {"MyObject":"{ \"MyKey1\" : \"Value1\", \"MyKey2\" : \"Value2\" }"} |
---|
select PKEY, JSON_OBJECT('MyObject' value TREAT(JSON_COLUMN as JSON)) JSON
from JSON_EXAMPLE1
PKEY | JSON | 1 | {"MyObject":{ "MyKey1" : "Value1", "MyKey2" : "Value2" }} |
---|