col user for a10 col PONumber for a20 col Sins for a20 col address for a20 SELECT a.document."User", a.document.PONumber, a.document."Special Instructions" AS Sins, a.document.address AS address FROM json_test a WHERE a.document."Special Instructions" IS NULL AND a.document.address IS NULL;
User PONUMBER SINS ADDRESS ---------- -------------------- -------------------- -------------------- ABULL 1600
SELECT a.document."User", a.document.PONumber, a.document."Special Instructions" AS Sins FROM json_test a WHERE a.document."Special Instructions" IS NULL and json_exists(a.document, '$."Special Instructions"' FALSE on error);
User PONUMBER SINS ---------- -------------------- -------------------- ABULL 1600
# 查询address元素不存在的数据
SELECT a.document."User", a.document.PONumber, a.document."Special Instructions" AS Sins FROM json_test a WHERE a.document."Special Instructions" IS NULL and not json_exists(a.document, '$.address' FALSE on error)
User PONUMBER SINS ---------- -------------------- -------------------- ABULL 1600
默认错误控制为FALSE ON ERROR,另外包含TRUE ON ERROR和ERROR ON ERROR,后者顾名思义表示不会获取任何通过JSON_EXISTS函数生成的错误。
JSON_VALUE
JSON_VALUE函数返回基于JSON路径查询JSON文档的元素
1 2 3 4 5 6 7 8
col user for a30 col costcenter for a30
select json_value(a.document, '$."User"') as "user",json_value(a.document, '$.CostCenter') as costcenter from json_test a;
user COSTCENTER ------------------------------ ------------------------------ ABULL A50
# 自定义错误控制 SQL> select json_value(a.document, '$.ShippingInstructions' ERROR ON ERROR) as ShippingInstructions from json_test a; select json_value(a.document, '$.ShippingInstructions' ERROR ON ERROR) as ShippingInstructions from json_test a * ERROR at line 1: ORA-40456: JSON_VALUE evaluated to non-scalar value
SQL> select a.document.ShippingInstructions from json_test a;
SHIPPINGINSTRUCTIONS ---------------------------------------------------------------------------------------------------- {"name":"Alexis Bull","Address":{"street":"200 Sporting Green","city":"South San Francisco","state": "CA","zipCode":99236,"country":"United States of America"},"Phone":[{"type":"Office","number":"909-5 55-7307"},{"type":"Mobile","number":"415-555-1234"}]}
insert into json_test values(sys_guid(), '{"active": false}'); insert into json_test values(sys_guid(), '{"active": true}'); commit;
col active for a20 select id,json_value(a.document,'$.active') as active,json_value(a.document,'$.active' returning number) as activenum from json_test a;
ID ACTIVE ACTIVENUM -------------------------------- -------------------- ---------- 9B225A54605370F8E0536892A8C0616F false 0 9B225A54605470F8E0536892A8C0616F true 1 9AFA0E321B132246E0536892A8C0BFD5
col REFERENCE for a20 col REQUESTOR for a50 select json_value(a.document,'$.Reference' returning varchar2) as Reference, json_value(a.document,'$.Requestor' returning varchar2(50)) as Requestor from json_test a;
create or replace view v_json as select jt.phone,jt.refrence,jt.name,jt.city,jt.quantity from json_test,json_table(document,'$' COLUMNS (phone varchar2(50 CHAR) PATH '$.PONumber', refrence varchar2(50 CHAR) PATH '$.Reference', name varchar2(50 CHAR) PATH '$.ShippingInstructions.name', city varchar2(50 CHAR) PATH '$.ShippingInstructions.Address.city', quantity varchar2(50 CHAR) PATH '$.LineItems.Quantity' )) jt where id='9AFA0E321B132246E0536892A8C0BFD5';
视图已创建。
col phone for a20 col refrence for a20 col name for a20 col city for a20 col quantity for a20
select * from v_json;
PHONE REFRENCE NAME CITY QUANTITY -------------------- -------------------- -------------------- -------------------- -------------------- 1600 ABULL-20140421 Alexis Bull South San Francisco
col phone for a20 col refrence for a20 col name for a20 col city for a20 col items for a50
select jt.* from json_test,json_table(document,'$' COLUMNS (phone varchar2(50 CHAR) PATH '$.PONumber', refrence varchar2(50 CHAR) PATH '$.Reference', name varchar2(50 CHAR) PATH '$.ShippingInstructions.name', city varchar2(50 CHAR) PATH '$.ShippingInstructions.Address.city', items varchar2(2000 CHAR) format json with wrapper PATH '$.LineItems' )) jt where id='9AFA0E321B132246E0536892A8C0BFD5';
PHONE REFRENCE NAME CITY ITEMS -------------------- -------------------- -------------------- -------------------- -------------------------------------------------- 1600 ABULL-20140421 Alexis Bull South San Francisco [[{"ItemNumber":1,"Part":{"Description":"One Magic Christmas","UnitPrice":19.95,"UPCCode":1313109289 9},"Quantity":9.0},{"ItemNumber":2,"Part":{"Descri ption":"Lethal Weapon","UnitPrice":19.95,"UPCCode" :85391628927},"Quantity":5.0}]]