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

参考

相关推荐
liweiweili1267 小时前
基于金庸武侠小说人物关系设计的完整 SQL 语句,包括数据库创建、表结构定义和示例数据插入
数据库·sql·oracle
我希望的一路生花10 小时前
Boris FX Samplitude Suite 2025.0.0 音频录制/编辑和母带处理
大数据·3d·oracle·sqlite·音视频·数据库开发
我不是星海16 小时前
Redis实战-优惠券秒杀解决方案总结大全
java·数据库·oracle
btyzadt16 小时前
记一次MySQL数据库的操作练习
数据库·mysql·oracle
HH思️️无邪1 天前
Flutter 开发技巧 AI 快速构建 json_annotation model 的提示词
flutter·json
客家族_Shark曾_小凡仙1 天前
关于国产 RAC 和分布式研讨
oracle·国产数据库
闲人编程1 天前
PyQt6 进阶篇:构建现代化、功能强大的桌面应用
数据库·python·oracle·gui·脚本·pyqt6·软件
GISBox1 天前
GIS新手入门首选!GISBox中文界面+一键安装,零依赖轻松搞定三维数据发布
vue.js·json·gis
MonkeyKing_sunyuhua1 天前
什么是JSON-RPC 2.0,在项目中应该怎么使用
qt·rpc·json
tan77º1 天前
【项目】分布式Json-RPC框架 - 抽象层与具象层实现
linux·服务器·c++·分布式·tcp/ip·rpc·json