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
--------------------------------------------------------------------------------
[]

参考

相关推荐
zhangxueyi1 小时前
SQL语言版本的差异、技术前景
数据库·sql·mysql·oracle
Run Out Of Brain1 小时前
使用Oracle的RPM包在Linux上安装MYSQL
linux·mysql·oracle
LBJ辉10 小时前
第 23 章 JSON
开发语言·前端·javascript·json·ecmascript
乐平要加油啊17 小时前
YOLO分割数据集转化(json转TXT)
yolo·json
Evaporator Core18 小时前
深入探索 SQLite:高级功能解析
数据库·oracle·sqlite
睿思达DBA_WGX21 小时前
Oracle 11g rac + Dataguard 环境调整 redo log 大小
数据库·oracle
无炘21 小时前
论述数据、数据库、数据库管理系统、数据库系统的概念。
数据库·oracle
大兵编码21 小时前
linux命令行连接Postgresql常用命令
linux·postgresql·oracle
x-cmd1 天前
[241231] CachyOS 2024 年终总结:性能飞跃与社区繁荣 | ScyllaDB 宣布转向开源可用许可证
linux·开源·操作系统·database·开发·集群·arch
yuanbenshidiaos2 天前
MYSQL------sql基础
sql·mysql·oracle