当前位置:网站首页>Oracle和JSON的结合
Oracle和JSON的结合
2022-07-01 10:49:00 【bisal(Chen Liu)】
很多人对JSON不陌生,JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,他基于ECMAScript(European Computer Manufacturers Association,欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得JSON成为理想的数据交换语言。易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
简而言之,JSON是一种存储和交换数据的语法。JSON本质就是字符串,JSON数据是在语言里面使用的,无论是Java、C#,还是js,都可以使用JSON。
JSON就是一组键值对的数据对象,在JavaScript里面就是JavaScript的对象,在Java中就是Java的JSON对象(类/VO)。JSON数据写为名称/值对,名称/值由字段名称构成,后跟冒号和值,一般的格式,如下所示,
name={name01:“test1”,name02={ name02:“test2”,name021:“test3”} }在JSON中,值必须是以下数据类型之一,
字符串
数字
对象(JSON对象)
数组
布尔
Null
JSON的值不可以是以下数据类型之一,
函数
日期
undefined
有开发经验的朋友可能以前用过XML作为数据传输的格式,如下就是一个XML数据格式的例子,

这是用JSON展示相同数据的例子,

可以看到,JSON简单的语法格式和清晰的层次结构明显要比XML容易阅读,并且在数据交换方面,由于JSON所使用的字符要比XML少得多,可以大大得节约传输数据所占用的带宽,传输效率上,会高于XML。
Oracle从12c开始就支持JSON的存储和检索,每个版本中都对相关的功能有所增强,例如一些JSON的限制,允许的长度等特性就会有改进,例如12c的描述中,JSON search index支持64字节,
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/oracle-json-restrictions.html#GUID-1DB81125-54A7-4CB6-864B-78E0E7E407C9

19c的描述中,JSON search index已经能支持255字节,
https://docs.oracle.com/en/database/oracle/oracle-database/19/adjsn/oracle-json-restrictions.html#GUID-1DB81125-54A7-4CB6-864B-78E0E7E407C9

所以当我们使用这些特性的时候,需要关注不同版本的支持差别。
Oracle中我们可以用json_object()函数来将检索结果转成JSON,如下所示,

还可以自定义key,

可以将检索结果存储为列表,

还支持做排序,

只使用json_object(),每条记录是单独的一个json,如果要将所有的记录生成一个json,可以结合json_arrayagg一起用,如下所示,

但很可能出现这个错误,返回记录数据多,提示最大值4000,

这是因为json_arrayagg()返回的类型有两种,varchar2和clob,如果不显式指定,用的就是varchar2,如果超过限度,可以改用clob类型,就会输出更多的内容,
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAYAGG.html#GUID-6D56077D-78DE-4CC0-9498-225DDC42E054

增加returning clob子句,就可以显示了,

和JSON相关的函数还有很多,有需要可以了解,

对于一些基本的JSON操作需求,Oracle还是可以满足的,这就避免了通过程序或者SQL拼接JSON的操作,从便利性上,有一定的帮助。
参考资料,
https://baike.baidu.com/item/JSON/2462549?fr=aladdin
https://blog.csdn.net/weixin_44600235/article/details/107630279
近期更新的文章:
《小白学习MySQL - 增量统计SQL的需求 - 开窗函数的方案》
近期的热文:
文章分类和索引:
边栏推荐
- 程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
- Detailed explanation of linear regression in machine learning
- Button button clear border
- The exclusive collection of China lunar exploration project is limited to sale!
- 106. 从中序与后序遍历序列构造二叉树
- LeetCode 438. 找到字符串中所有字母异位词__滑动窗口
- SQL optimization - in and not in, exist
- prism journal导航按钮的可用性探索记录
- Mobile hard drive reads but does not display drive letter
- 2022年已经过去一半了,是不是很突然呢?
猜你喜欢
![[encounter Django] - (II) database configuration](/img/23/aed472757f7e238a146b043c0405d7.png)
[encounter Django] - (II) database configuration

Venv: directory structure of venv

mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)

Handling distributed transactions with powerful dbpack (PHP tutorial)

谷歌新论文-Minerva:用语言模型解决定量推理问题

CRC verification

Matplotlib data visualization Foundation

NC | 肠道细胞和乳酸菌共同作用来防止念珠菌感染

The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion

【MPC】①二次规划问题MATLAB求解器quadprog
随机推荐
Venv: directory structure of venv
北汽蓝谷:业绩承压,极狐难期
CRC verification
bash: ln: command not found
[paper reading] trajectory guided control prediction for end to end autonomous driving: a simple yet strong Ba
云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
选择在中金证券上炒股开户可以吗?安全吗?
数据库实验报告(二)
Database experiment report (I)
442. duplicate data in array
网站源码整站下载 网站模板源代码下载
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
[MPC] ② quadprog solves positive definite, semi positive definite and negative definite quadratic programming
NeurIPS 2022 | 细胞图像分割竞赛正式启动!
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
A new round of popularity of digital collections opens
[encounter Django] - (II) database configuration