JSON_VALUE, JSON_QUERY & JSON_TABLE function use with example in Oracle Database
What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format. It is easy for humans to read and write. Handle JSON data into the Oracle. Oracle provides various function to store and handle JSON data in quick and easy manners:
Insert JSON data into Oracle table
-- Create a table which insert json datacreate table json_test ( id number, data clob );-- Insert the JSON formated data into the data clob columninsert into json_test values (101,'{region_id: 1, region_name: "Europe"}');insert into json_test values (102,'{region_id: 2, region_name: "Americas"}');insert into json_test values (103,'{region_id: 3}');-- Access the json data in oracleSELECT * FROM json_test; ID DATA---------- ----------------------------------------- 101 {region_id: 1, region_name: "Europe"} 102 {region_id: 2, region_name: "Americas"}
Use of IS JSON statement
It will return data that is in JSON format and leave rows that are not in JSON format as shown below:
INSERT INTO json_test VALUES (104, 'Sunny');SQL> select id,data from json_test where data is json; ID DATA---------- ------------------------------------------ 101 {region_id: 1, region_name: "Europe"} 102 {region_id: 2, region_name: "Americas"} 103 {region_id: 3}
Use of JSON_VALUE function
JSON_VALUE function returns an element from the JSON data based on the specified JSON path.
col regionid for a10col regionname for a10SELECT JSON_VALUE(data, '$.region_id') AS regionid, JSON_VALUE(data, '$.region_name') AS regionnameFROM json_test;REGIONID REGIONNAME---------- ----------1 Europe2 AmericasSQL> insert into json_test values (103,'{region_id: 3}');1 row created.SQL> SELECT JSON_VALUE(data, '$.region_id') AS regionid, JSON_VALUE(data, '$.region_name') AS regionname FROM json_test;REGIONID REGIONNAME---------- ----------1 Europe2 Americas3
Error handling in JSON VALUE function
Syntax: { ERROR | NULL | DEFAULT literal } ON ERROR
-- Inserted one more row which having no json dataINSERT INTO json_test VALUES (104, 'Sunny');SELECT id,JSON_VALUE(data, '$.region_id') AS regionid, JSON_VALUE(data, '$.region_name') AS regionname FROM json_test; ID REGIONID REGIONNAME---------- ---------- ---------- 101 1 Europe 102 2 Americas 103 3 104Note: JSON_VALUE not throw error it return null if table has non json valueSELECT id,JSON_VALUE(data, '$.region_id') AS regionid, JSON_VALUE(data, '$.region_name' ERROR ON ERROR) AS regionname FROM json_test;ERROR:ORA-40462: JSON_VALUE evaluated to no value
JSON_VALUE we can reset or convert the datatype as follows:
col regionid for 999SELECT id,JSON_VALUE(data, '$.region_id' returning number) AS regionid, JSON_VALUE(data, '$.region_name') AS regionname FROM json_test; ID REGIONID REGIONNAME---------- -------- ---------- 101 1 Europe 102 2 Americas 103 3 104
JSON_QUERY function
JSON_QUERY function returns one or more values as shown below:
insert into json_test values (105,'{region_id: 5, region_name: { country_name: "Americas",city_name:"New York"}}'); SELECT id,JSON_VALUE(data, '$.region_id' returning number) AS regionid, JSON_VALUE(data, '$.region_name') AS regionname FROM json_test; ID REGIONID REGIONNAME---------- -------- ---------- 101 1 Europe 102 2 Americas 103 3 104 105 5 -- Json query will return and add square bracket to its outputSELECT id, JSON_QUERY(data, '$.region_name' WITH WRAPPER) AS country_detail FROM json_test; ID COUNTRY_DETAIL---------- -------------------------------------------------- 101 ["Europe"] 102 ["Americas"] 103 104 105 [{"country_name":"Americas","city_name":"New York" }]
Use of JSON_TABLE
Represents the JSON data in the form of the table
JSON_TABLE Examplecol region_id for a3col region_name for a15col country_name for a15col city_name for a15SELECT jt.region_id, jt.region_name, jt.country_name, jt.city_nameFROM json_test, JSON_TABLE(data, '$' COLUMNS (region_id VARCHAR2(50 CHAR) PATH '$.region_id', region_name VARCHAR2(50 CHAR) PATH '$.region_name', country_name VARCHAR2(10 CHAR) PATH '$.region_name.country_name', city_name VARCHAR2(50 CHAR) PATH '$.region_name.city_name' )) jt; REG REGION_NAME COUNTRY_NAME CITY_NAME--- --------------- --------------- ---------------1 Europe2 Americas35 Americas New York
JSON data identify in Oracle
In Oracle, identify the list of columns using JSON data by the following view:
SELECT table_name,
column_name,
format,
data_type
FROM dba_json_columns;