SQL?Server中JSON函數(shù)的用法詳解
SQL Server 2005開始支持XML數(shù)據(jù)類型,提供原生的XML數(shù)據(jù)類型、XML索引及各種管理或輸出XML格式的函數(shù)。
隨著JSON的流行,SQL Server 2016開始支持JSON數(shù)據(jù)類型,不僅可以直接輸出JSON格式的結(jié)果集,還能讀取JSON格式的數(shù)據(jù)。
官方文檔:https://docs.microsoft.com/zh-cn/sql/relational-databases/json/json-data-sql-server?view=sql-server-2017
下面是我們熟悉的SELECT及輸出格式,后面對JSON的演示基于此SQL:

一、 將查詢結(jié)果輸出JSON格式
1、FOR JSON AUTO:SELECT語句的結(jié)果以JSON輸出。
要將SELECT語句的結(jié)果以JSON輸出,最簡單的方法是在后面加上FOR JSON AUTO:

2、FOR JSON AUTO,Root(’’) :為JOSN加上根節(jié)點
若要為FOR JSON加上Root Key,可以用ROOT選項來自定義ROOT 節(jié)點的名稱:

3、FOR JSON PATH輸出:可通過列別名來定義JSON對象的層次結(jié)構(gòu)
若要自定義輸出JSON格式的結(jié)構(gòu)時,必須使用JSONPATH。
- FOR JSON Auto,自動按照查詢語句中使用的表結(jié)構(gòu)來創(chuàng)建嵌套的JSON子數(shù)組,類似于For Xml Auto特性。
- FOR JSON Path,通過列名或者列別名來定義JSON對象的層次結(jié)構(gòu),列別名中可以包含“.”,JSON的成員層次結(jié)構(gòu)將會與別名中的層次結(jié)構(gòu)保持一致。
這個特性非常類似于早期SQL Server版本中的For Xml Path子句,可以使用斜線來定義xml的層次結(jié)構(gòu)。

4、FOR JSON PATH+ROOT輸出:為JOSN加上根節(jié)點

5、INCLUDE_NULL_VALUES:值null的字段需要顯示出現(xiàn)。
為NULL的數(shù)據(jù)在輸出JSON時,會被忽略,若想要讓NULL的字段也顯示出來,可以加上選項INCLUDE_NULL_VALUES,該選項也適用于AUTO。

6、列的別名,可以增加帶有層級關(guān)系的節(jié)點。
比如下面的SQL,增加了一個“SN”節(jié)點,把欄位SERNUM和CLIMAT放在里面:

演示實例:
select TOP (2) id, Plies, Createtime from [dbo].[B3PliesData] ORDER BY ID ;
--1178 3 2020-07-21 14:33:18.480
--1179 3 2020-07-21 14:36:27.457
select TOP (2) id, Plies as [myObject.Plies], Createtime as [myObject.Createtime] from [dbo].[B3PliesData] ORDER BY ID for json auto;
--[{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]
select TOP (2) id, Plies, Createtime from [dbo].[B3PliesData] ORDER BY ID for json auto ,root('myRoot') ;
--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}]}
select TOP (2) id, Plies as [myObject.Plies], Createtime as [myObject.Createtime] from [dbo].[B3PliesData] ORDER BY ID for json path;
--[{"id":1178,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:33:18.480"}},{"id":1179,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}}]
select TOP (2) id, Plies, Createtime,null as mynull from [dbo].[B3PliesData] ORDER BY ID for json path,root('myRoot');
--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480"},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}]}
select TOP (2) id, Plies, Createtime,null as mynull from [dbo].[B3PliesData] ORDER BY ID for json path,root('myRoot'),include_null_values;
--{"myRoot":[{"id":1178,"Plies":3,"Createtime":"2020-07-21T14:33:18.480","mynull":null},{"id":1179,"Plies":3,"Createtime":"2020-07-21T14:36:27.457","mynull":null}]}二、 解析JSON格式的數(shù)據(jù)
1、使用OPENJSON()函數(shù):

2、通過WITH選項,自定義輸出列:

實例演示:
-------------1、-------------
declare @json as varchar(8000)
set @json='[
{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},
{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]'
select * from openjson(@json);
--key value type
--0 {"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"} 5
--1 {"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"} 5
-------------2、-------------
declare @json1 as varchar(8000)
set @json1='[
{"id":1178,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:33:18.480"},
{"id":1179,"myObject.Plies":3,"myObject.Createtime":"2020-07-21T14:36:27.457"}]
'
select * from openjson(@json1)
with(
id varchar(10) '$.id',
Plies int '$."myObject.Plies"',
Createtime datetime '$."myObject.Createtime"'
);
--id Plies Createtime
--1178 3 2020-07-21 14:33:18.480
--1179 3 2020-07-21 14:36:27.457
-------------3、-------------
declare @json2 as varchar(8000)
set @json2='{"myRoot":[
{"id":1178,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:33:18.480"}},
{"id":1179,"myObject":{"Plies":3,"Createtime":"2020-07-21T14:36:27.457"}}
]}'
select * from openjson(@json2,'$.myRoot')
with(
id varchar(10) ,
Plies int '$.myObject.Plies',
Createtime datetime '$.myObject.Createtime'
);
--id Plies Createtime
--1178 3 2020-07-21 14:33:18.480
--1179 3 2020-07-21 14:36:27.457三、JSON函數(shù)
declare @param nvarchar(max);
set @param = N'{
"info":{
"type":1,
"address":{
"town":"Bristol",
"county":"Avon",
"country":"England"
},
"tags":["Sport", "Water polo"]
},
"type":"Basic"
}';1、ISJSON:測試字符串是否包含有效 JSON。
print iif(isjson(@param) > 0, 'OK', 'NO');
返回:OK
2、JSON_VALUE :從 JSON 字符串中提取標(biāo)量值。
print json_value(@param, '$.info.address.town'); print json_value(@param, '$.info.tags[1]');
返回:Bristol,Water polo
3、JSON_QUERY :從 JSON 字符串中提取對象或數(shù)組。返回類型為 nvarchar(max) 的 JSON 片段
print json_query(@param, '$.info');
{
"type":1,
"address":{
"town":"Bristol",
"county":"Avon",
"country":"England"
},
"tags":["Sport", "Water polo"]
}4、JSON_MODIFY :更新 JSON 字符串中屬性的值,并返回已更新的 JSON 字符串。
print json_modify(@param, '$.info.address.town', 'London');
返回:
{
"info":{
"type":1,
"address":{
"town":"London",
"county":"Avon",
"country":"England"
},
"tags":["Sport", "Water polo"]
},
"type":"Basic"
}實例演示:
declare @param nvarchar(max);
set @param=N'{
"info":{
"type":1,
"address":{
"town":"Bristol",
"county":"Avon",
"country":"England"
},
"tags":["Sport", "Water polo"]
},
"type":"Basic"
}';
print iif(isjson(@param)>0, 'OK', 'NO');
print json_query(@param);
print json_value(@param, '$.info.address.town');
print json_value(@param, '$.info.tags[1]');
print json_query(@param, '$.info');
print json_query('["2020-1-8","2020-1-9"]');
print json_modify(@param, '$.info.address.town', 'London');四、注意事項
SQL2016 中的新增的內(nèi)置JSON進(jìn)行了簡單介紹,主要有如下要點:
- JSON能在SQLServer2016中高效的使用,但是JSON并不是原生數(shù)據(jù)類型;
- 如果使用JSON格式必須為輸出結(jié)果是表達(dá)式的提供別名;
- JSON_VALUE 和 JSON_QUERY 函數(shù)轉(zhuǎn)移和獲取Varchar格式的數(shù)據(jù),因此必須將數(shù)據(jù)轉(zhuǎn)譯成你需要的類型。
- 在計算列的幫助下查詢JSON可以使用索引進(jìn)行優(yōu)化。
到此這篇關(guān)于SQL Server使用JSON函數(shù)的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信