当前位置:网站首页>Oracle和JSON的結合
Oracle和JSON的結合
2022-07-01 10:51: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的需求 - 開窗函數的方案》
近期的熱文:
文章分類和索引:
边栏推荐
- The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion
- 12款大家都在用的产品管理平台
- CRC verification
- Venv: directory structure of venv
- Is the securities account opened by Yixue school for individuals safe? Is there a routine
- 大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
- [.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- 使用强大的DBPack处理分布式事务(PHP使用教程)
- Have you learned the necessary global exception handler for the project
- Dotnet console uses microsoft Maui. Getting started with graphics and skia
猜你喜欢

基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建

NC | intestinal cells and lactic acid bacteria work together to prevent Candida infection

New situation of digital collection market

Uncover the secrets of new products! Yadi Guanneng 3 multi product matrix to meet the travel needs of global users

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

使用强大的DBPack处理分布式事务(PHP使用教程)

Database experiment report (II)

Wireshark TS | confusion between fast retransmission and out of sequence

The exclusive collection of China lunar exploration project is limited to sale!

How does MySQL copy table data from one database to another (two databases are not linked to the same database)
随机推荐
数据库实验报告(二)
mysql如何把 一个数据库中的表数据 复制到 另一个数据库中(两个数据库不在同一个数据库链接下)
【Matytype】在CSDN博客中插入Mathtype行间与行内公式
中国探月工程独家藏品限量发售!
Mall applet source code open source version - two open
【MPC】②quadprog求解正定、半正定、负定二次规划
[MPC] ① quadratic programming problem matlab solver quadprog
网站源码整站下载 网站模板源代码下载
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
The list of winners of the digital collection of "century master" was announced
12款大家都在用的产品管理平台
Prism journal navigation button usability exploration record
想请教一下,我在广州,到哪里开户比较好?现在网上开户安全么?
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
基金管理人的合规管理
移动硬盘驱动器读到,但不显示盘符
Rising Stars in Plant Sciences (RSPS2022) Finalist科学演讲会(6.30晚9点)
谷歌新论文-Minerva:用语言模型解决定量推理问题
数字藏品市场新局面
Valgrind usage of memory leak locating tool