Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用交叉应用查询json中的嵌套数组_Sql_Json_Sql Server_Tsql_Sql Server 2016 - Fatal编程技术网

Sql 使用交叉应用查询json中的嵌套数组

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”。当我向查询中添加另一个交叉

我有可能是一个相对简单的查询,但我无法了解如何查询嵌套的json数组。我有一个带有json字段的SQL 2016 DB,其中包含一个带有多个子数组的json字符串

附件是json数据的图像:

我想查询“身份”数据(9999829250103)

我可以使用以下查询查询accountId(LOADACC001)或昵称(LoadTest)等数据,但无法从“account”数组中查询:


字段“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