JSON 系列之5:JSON_QUERY

上一篇文章介绍了JSON_VALUE,他接受一个JSON输入,输出一个SQL数据类型的标量。

JSON_QUERY也是接受一个JSON输入,但输出的仍是JSON数据类型。

还是通过示例来学习吧。

创建测试表,插入2条数据:

sql 复制代码
drop table customerTab;

CREATE TABLE
customerTab (custData VARCHAR2(4000),
CONSTRAINT ensure_json2 CHECK (custData IS
JSON STRICT));

CREATE TABLE
customerTab (custData JSON);

INSERT INTO
customerTab
VALUES ('{
"custNo":2,
"name" : "Jane",
"status":"Gold",
"address": {
"Street": "Main Rd 1",
"City": "San Jose",
"zip": 95002
}}');

INSERT INTO
customerTab
VALUES ('{
"custNo":3,
"name" : "Jill",
"status":["Important","Gold"],
"address": {
"Street": "Broadway 2",
"City": "Belmont",
"zip": 94065
}}');

commit;

查询地址,返回的是JSON对象:

sql 复制代码
SQL> 
SELECT
JSON_QUERY(custData, '$.address')
FROM
customerTab;

JSON_QUERY(CUSTDATA,'$.ADDRESS')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
---------------------------------------------------------------------------------
{"Street":"Main Rd 1","City":"San Jose","zip":95002}
{"Street":"Broadway 2","City":"Belmont","zip":94065}

查询状态,返回的第2列是数组,但第一列不是:

sql 复制代码
SQL>
SELECT
JSON_QUERY(custData, '$.status')
FROM customerTab;

JSON_QUERY(CUSTDATA,'$.STATUS')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
-----------------------------------------------------------------------
"Gold"
["Important","Gold"]

如果希望都返回数组,可以使用ARRAY WRAPPER子句:

sql 复制代码
SQL>
SELECT
JSON_QUERY(custData, '$.status' WITH ARRAY WRAPPER)
FROM customerTab;

JSON_QUERY(CUSTDATA,'$.STATUS'WITHARRAYWRAPPER)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
-------------------------------------------------------------------
["Gold"]
[["Important","Gold"]]

SQL>
SELECT
JSON_QUERY(custData, '$.status' WITH CONDITIONAL ARRAY WRAPPER)
FROM customerTab;

JSON_QUERY(CUSTDATA,'$.STATUS'WITHCONDITIONALARRAYWRAPPER)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
--------------------------------------------------------------------
"Gold"
["Important","Gold"]

再插入一条带多个地址的数据:

sql 复制代码
INSERT INTO
customerTab
VALUES ('{
"custNo":3,
"name" : "Jim",
"status": "Silver",
"address":[
{
"Street": "Fastlane 4",
"City": "Atherton",
"zip": 94027
},
{
"Street": "Slowlane 5",
"City": "San Francisco",
"zip": 94105
} ]
}');

commit;

查询地址,可以看出有无ARRAY WRAPPER的区别:

sql 复制代码
SQL>
SELECT
JSON_QUERY(custData, '$.address[*].City' )
FROM customerTab;

JSON_QUERY(CUSTDATA,'$.ADDRESS[*].CITY')                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
-----------------------------------------------
"San Jose"
"Belmont"


已选择 3 行。

SQL>
SELECT
JSON_QUERY(custData, '$.address[*].City' WITH ARRAY WRAPPER)
FROM customerTab;

JSON_QUERY(CUSTDATA,'$.ADDRESS[*].CITY'WITHARRAYWRAPPER)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
-------------------------------------------------------------------
["San Jose"]
["Belmont"]
["Atherton","San Francisco"]

已选择 3 行。

实际上*可以省略,下面SQL等同于上面的SQL:

sql 复制代码
SELECT
JSON_QUERY(custData, '$.address.City' WITH ARRAY WRAPPER)
FROM customerTab;

*表示所有,也可以指定范围:

sql 复制代码
SELECT
JSON_QUERY(custData, '$.address[0 to 1].City' WITH ARRAY WRAPPER)
FROM customerTab;

接下来看官网上的例子

sql 复制代码
-- 注意,输入是Lax Syntax,输出则是Strict JSON Syntax
SELECT JSON_QUERY('{a:100, b:200, c:300}', '$') AS value
  FROM DUAL;

VALUE
---------------------------------------------------------------------------
{"a":100,"b":200,"c":300}

SELECT JSON_QUERY('{a:100, b:200, c:300}', '$.a' WITH WRAPPER) AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[100]

SELECT JSON_QUERY('{a:100, b:200, c:300}', '$.*' WITH WRAPPER) AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[100,200,300]

SELECT JSON_QUERY('[0,1,2,3,4]', '$') AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[0,1,2,3,4]

SELECT JSON_QUERY('[0,1,2,3,4]', '$' WITH WRAPPER) AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[[0,1,2,3,4]]

SELECT JSON_QUERY('[0,1,2,3,4]', '$[*]' WITH WRAPPER) AS value
  FROM DUAL;

VALUE
-----------------------------------------------------------------------------
[0,1,2,3,4]

SELECT JSON_QUERY('[0,1,2,3,4,5,6,7,8]', '$[0, 3 to 5, 7]' WITH WRAPPER) AS value
  FROM DUAL;

VALUE
--------------------------------------------------------------------------------
[0,3,4,5,7]

SELECT JSON_QUERY('[{a:100},{b:200},{c:300}]', '$[0]'
       WITH CONDITIONAL WRAPPER) AS value
  FROM DUAL;

VALUE
--------------------------------------------------------------------------------
{"a":100}

-- 若无WITH CONDITIONAL WRAPPER,则返回null
SELECT JSON_QUERY('[{"a":100},{"b":200},{"c":300}]', '$[*]'
       WITH CONDITIONAL WRAPPER) AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[{"a":100},{"b":200},{"c":300}]

-- 和上例类似,只是返回的是SQL数据类型,而非JSON
SELECT JSON_QUERY('[{"a":100},{"b":200},{"c":300}]', '$[*]'
       RETURNING VARCHAR2(100) WITH CONDITIONAL WRAPPER) AS value
  FROM DUAL;

VALUE
----------------------------------------------------------------------------
[{"a":100},{"b":200},{"c":300}]

-- EMPTY ON ERROR 为默认,其实可以不写。可以改为ERROR ON ERROR试试
SELECT JSON_QUERY('[{"a":100},{"b":200},{"c":300}]', '$[3]'
       EMPTY ON ERROR) AS value
  FROM DUAL;

VALUE
--------------------------------------------------------------------------------
[]

参考

相关推荐
JhonKI3 小时前
从零实现Json-Rpc框架】- 项目实现 - 服务端registry&discovery实现
网络协议·rpc·json
小陈又菜8 小时前
MySQL-触发器
数据库·mysql·database·触发器
爱的叹息8 小时前
详解隔离级别(4种),分别用表格展示问题出现的过程及解决办法
数据库·oracle
东方佑9 小时前
使用Python解析PPT文件并生成JSON结构详解
python·json·powerpoint
Arbori_262159 小时前
oracle常用sql
数据库·sql·oracle
inxunoffice13 小时前
批量修改记事本文本文件编码,可以解决文本文件乱码问题
json
Kaede620 小时前
怎么安装JSON服务器?JSON服务器最新安装教程
运维·服务器·json
Tyler先森21 小时前
Oracle数据库数据编程SQL<3.5 PL/SQL 存储过程(Procedure)>
数据库·sql·oracle
Albert Tan1 天前
Oracle 10G DG 修复从库-磁盘空间爆满导致从库无法工作
数据库·oracle
还是鼠鼠1 天前
Node.js Express 处理静态资源
前端·javascript·vscode·node.js·json·express