Sql 使用交叉应用查询json中的嵌套数组
我有可能是一个相对简单的查询,但我无法了解如何查询嵌套的json数组。我有一个带有json字段的SQL 2016 DB,其中包含一个带有多个子数组的json字符串 附件是json数据的图像: 我想查询“身份”数据(9999829250103) 我可以使用以下查询查询accountId(LOADACC001)或昵称(LoadTest)等数据,但无法从“account”数组中查询:Sql 使用交叉应用查询json中的嵌套数组,sql,json,sql-server,tsql,sql-server-2016,Sql,Json,Sql Server,Tsql,Sql Server 2016,我有可能是一个相对简单的查询,但我无法了解如何查询嵌套的json数组。我有一个带有json字段的SQL 2016 DB,其中包含一个带有多个子数组的json字符串 附件是json数据的图像: 我想查询“身份”数据(9999829250103) 我可以使用以下查询查询accountId(LOADACC001)或昵称(LoadTest)等数据,但无法从“account”数组中查询: 字段“BankDataText”包含json字符串,该表称为“transactions”。当我向查询中添加另一个交叉
字段“BankDataText”包含json字符串,该表称为“transactions”。当我向查询中添加另一个交叉应用程序时,不会返回任何行。您的思路正确,只需将嵌套的json节提取为json,然后使用另一个
交叉应用程序打开json
读取内部帐户
节:
declare @tmp table ([id] int, BankDataText nvarchar(max))
declare @j nvarchar(max)='{
"data": [{
"account": {
"account": [{
"schemaName": "SortCodeAccountNumber",
"identification": "99999829250103",
"name": "Load testing ....",
"secondaryIdentification": "123456789"
}],
"accountType": "null",
"accountSubType": "null",
"description": null,
"accountId": "LOADACC001",
"currency": "GBP",
"nickname": "LoadTest",
"servicer": {
"schemaName": "BICFI",
"identification": "PAPAUK00XXX"
}
}
}]
}'
insert into @tmp select 675, @j
select top 1
A.accountId as NonUserAccountId, B.identification
from @tmp t
cross apply openjson (t.BankDataText, N'$.data') with
(
accountId varchar(100) 'strict $.account.accountId',
account nvarchar(max) 'strict $.account.account' as json
) A
cross apply openjson(A.account) with
(
identification varchar(100)
) B
结果:
请以文本(非图像)格式发布整个json
declare @tmp table ([id] int, BankDataText nvarchar(max))
declare @j nvarchar(max)='{
"data": [{
"account": {
"account": [{
"schemaName": "SortCodeAccountNumber",
"identification": "99999829250103",
"name": "Load testing ....",
"secondaryIdentification": "123456789"
}],
"accountType": "null",
"accountSubType": "null",
"description": null,
"accountId": "LOADACC001",
"currency": "GBP",
"nickname": "LoadTest",
"servicer": {
"schemaName": "BICFI",
"identification": "PAPAUK00XXX"
}
}
}]
}'
insert into @tmp select 675, @j
select top 1
A.accountId as NonUserAccountId, B.identification
from @tmp t
cross apply openjson (t.BankDataText, N'$.data') with
(
accountId varchar(100) 'strict $.account.accountId',
account nvarchar(max) 'strict $.account.account' as json
) A
cross apply openjson(A.account) with
(
identification varchar(100)
) B