CREATE TABLE json_data (my_doc CLOB)
Table created.
The JSON Newbie At Work
BEGIN
INSERT INTO json_data
VALUES (q'^{
"without_spaces" : "OK",
"with spaces not a good idea" : "Not OK",
"carriage returns definitely bad idea" :
"BEGIN
DBMS_OUTPUT.PUT_LINE ('Hello JSON!');
END;"}^');
COMMIT;
END;
1 row(s) inserted.
Get a JSON Value
SELECT json_value (j.my_doc, '$.without_spaces') r
FROM json_data j
R |
---|
OK |
SELECT json_value (j.my_doc, '$."without_spaces"') r
FROM json_data j
R |
---|
OK |
SELECT json_value (j.my_doc, '$.with spaces not a good idea') r
FROM json_data j
ORA-40442: JSON path expression syntax error ('$.with spaces not a good idea')More Details: https://docs.oracle.com/error-help/db/ora-40442
SELECT json_value (j.my_doc, '$."with spaces not a good idea"') r
FROM json_data j
R |
---|
Not OK |
SELECT json_value (j.my_doc, '$."carriage returns definitely bad idea"') r
FROM json_data j
R |
---|
- |
DELETE FROM json_data
1 row(s) deleted.
Ensure Good JSON Data!
ALTER TABLE json_data
ADD CONSTRAINT json_data_json_chk
CHECK (my_doc IS json) enable
Table altered.
Lo and Behold
BEGIN
INSERT INTO json_data
VALUES (q'^{
"without_spaces" : "OK",
"with spaces not a good idea" : "Not OK",
"carriage returns definitely bad idea" :
"BEGIN
DBMS_OUTPUT.PUT_LINE ('Hello JSON!');
END;"}^');
COMMIT;
END;
ORA-02290: check constraint (SQL_RSTISLHZJFIMPSEDOQMKBXEAW.JSON_DATA_JSON_CHK) violated ORA-06512: at line 2 ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02290
BEGIN
INSERT INTO json_data
VALUES (q'^{
"without_spaces" : "OK",
"with spaces not a good idea" : "Not OK",
"carriage returns definitely bad idea" :
"BEGIN\\nDBMS_OUTPUT.PUT_LINE ('Hello JSON!');\\nEND;"}^');
COMMIT;
END;
1 row(s) inserted.
SELECT json_value (j.my_doc, '$."carriage returns definitely bad idea"') r
FROM json_data j
R |
---|
BEGIN\nDBMS_OUTPUT.PUT_LINE ('Hello JSON!');\nEND; |
DROP TABLE json_data
Table dropped.