当前位置:网站首页>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的需求 - 開窗函數的方案》
近期的熱文:
文章分類和索引:
边栏推荐
- Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
- [.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B
- 零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
- Is it safe to buy funds on the access letter?
- 442. 数组中重复的数据
- 选择在中金证券上炒股开户可以吗?安全吗?
- 12 product management platforms that everyone is using
- Suggest collecting | what to do when encountering slow SQL on opengauss?
- 大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
猜你喜欢

新一代云原生数据库的设计与实践

LeetCode.515. 在每个树行中找最大值___逐一BFS+DFS+按层BFS

Matplotlib data visualization Foundation

网站源码整站下载 网站模板源代码下载

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

数据库的增删改查问题

Venv: directory structure of venv

What are the advantages and disadvantages of PHP
![[matytype] insert MathType inter line and intra line formulas in CSDN blog](/img/ff/871a3f06f898ed107a2a974d2c7bc4.png)
[matytype] insert MathType inter line and intra line formulas in CSDN blog

价值1000毕业设计校园信息发布平台网站源码
随机推荐
SQL optimization - in and not in, exist
数字藏品平台搭建需要注意哪些法律风险及资质?
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
Half of 2022 has passed, isn't it sudden?
Ask everyone in the group about the fact that the logminer scheme of flick Oracle CDC has been used to run stably in production
中国探月工程独家藏品限量发售!
bash: ln: command not found
LeetCode. One question of the day: offer II 091 Paint the house (DP problem)
dotnet 控制台 使用 Microsoft.Maui.Graphics 配合 Skia 进行绘图入门
Design and practice of new generation cloud native database
Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
Project0: Games
How do clients request databases?
Venv: directory structure of venv
Lack of comparator, operational amplifier to save the field! (the op amp is recorded as a comparator circuit)
基金管理人的内部控制
【Laravel 】faker数据填充详解
想开个户,在网上开华泰证券的户安全吗?
【邂逅Django】——(二)数据库配置