2

我有一些我想在 SQL Server 2016 中解析的 JSON。有一个带有数组的层次结构。我想编写一个更有效地解析整个层次结构的查询,我在尝试访问嵌入式数组时遇到了挑战,尤其是“ DealerPrefLocation ”下的任何内容,而我在“DealerInformation”下访问任何内容都没有问题,下面是我的示例 JSON:

        {
            "DealerInformation": {
                "Altername": [
                    {
                        "firstName": "two",
                        "lastName": "one",
                        "middleName": null,
                        "otherNameExplanation": "change"
                    }
                ],
                "DealerType": {
                    "id": "87ab-098ng-2345li",
                    "name": "DMD"
                },
                "firstName": "PK",
                "middleName": null,
                "lastName": "KPK",
                "primaryDealerState": "AP",
                "otherDealerState": [
                    "AP",
                    "MP"]
            },
            "DealerPrefLocation": [
                {
                    "PrefLocation": [
                        {
                            "address": {
                                "address1": "fort warangal",
                                "address2": "east",
                                "addressStandardizationSource": null,
                                "city": "warangal",
                                "country": "India"
                            },
                            "apptPhoneNumber": "989898989898",
                            "createdAt": null,
                            "phoneNumber": "989898989898"
                        }
                    ],
                    "NonPrefLocation": [
                        {
                            "address": {
                                "address1": "fort Junction",
                                "address2": null,
                                "addressStandardizationSource": null
                            },
                            "createdAt": null,
                            "ServiceName": "H1",
                            "ServiceId": [
                                {
                                    "ServiceGroupName": null,
                                    "Type": "GROUP",
                                    "ServiceNumber": "9999999"
                                }
                            ]
                        }
                    ],
                    "Inserted": null,
                    "Updated": null     }
            ]
        }

我确实想出了如何查询“DealerInformation”和其中的数组,例如“AlterName”和“OtherDealerState”,但是我在查询“DealerInformation”-->“PrefLocation”-->Address 下的数组时遇到了挑战。

请找到我当前的查询和输出:

select 
  ID,
  JSON_VALUE(VALUE_ID,'$.DealerInformation.firstName'),
  JSON_VALUE(VALUE_ID,'$.DealerInformation.primaryDealerState'),
  JSON_VALUE(A.VALUE,'$.firstName'),
  JSON_VALUE(C.VALUE,'$.PrefLocation.address.address1')
from 
  Test_JSON_File 
cross apply 
  openjson(Test_JSON_File.value_id,'$.DealerInformation.Altername')A 
cross apply 
  openjson(Test_JSON_File.Test_JSON_CAQH.value_id,'$.DealerPrefLocation')C

我选择的最后一列来自“DealerPrefLocation”,但我只得到空值,有人可以帮助我在 SQL 中缺少什么或者我需要添加什么?

4

1 回答 1

1

对不起,这个答案有点晚了......

我认为,对您来说最重要的信息是AS JSON在-WITH子句中。看看我如何使用它:

DECLARE @json NVARCHAR(MAX) =   
N' {
            "DealerInformation": {
                "Altername": [
                    {
                        "firstName": "two",
                        "lastName": "one",
                        "middleName": null,
                        "otherNameExplanation": "change"
                    }
                ],
                "DealerType": {
                    "id": "87ab-098ng-2345li",
                    "name": "DMD"
                },
                "firstName": "PK",
                "middleName": null,
                "lastName": "KPK",
                "primaryDealerState": "AP",
                "otherDealerState": [
                    "AP",
                    "MP"]
            },
            "DealerPrefLocation": [
                {
                    "PrefLocation": [
                        {
                            "address": {
                                "address1": "fort warangal",
                                "address2": "east",
                                "addressStandardizationSource": null,
                                "city": "warangal",
                                "country": "India"
                            },
                            "apptPhoneNumber": "989898989898",
                            "createdAt": null,
                            "phoneNumber": "989898989898"
                        }
                    ],
                    "NonPrefLocation": [
                        {
                            "address": {
                                "address1": "fort Junction",
                                "address2": null,
                                "addressStandardizationSource": null
                            },
                            "createdAt": null,
                            "ServiceName": "H1",
                            "ServiceId": [
                                {
                                    "ServiceGroupName": null,
                                    "Type": "GROUP",
                                    "ServiceNumber": "9999999"
                                }
                            ]
                        }
                    ],
                    "Inserted": null,
                    "Updated": null     }
            ]
        }';

——我会从每个区域中至少挑选一个元素。这应该为您指明方向:

SELECT B.firstName
      ,B.middleName
      ,B.lastName
      ,JSON_VALUE(B.DealerType,'$.id') AS DealerTypeId
      ,B.PrimaryDealerState
      ,B.otherDealerState                                          --You can dive deeper to parse that array
      ,JSON_VALUE(B.Altername,'$[0].firstName') AS Alter_firstName --there might be more...
      ,JSON_VALUE(C.PrefLocation,'$[0].address.address1') AS pref_address --there might be more...
      ,JSON_VALUE(C.PrefLocation,'$[0].apptPhoneNumber') AS pref_apptPhoneNumber
      ,JSON_VALUE(C.NonPrefLocation,'$[0].address.address1') AS nonpref_address --there might be more...
      ,JSON_VALUE(C.NonPrefLocation,'$[0].ServiceName') AS nonpref_ServiceName
FROM OPENJSON(@json)
WITH(DealerInformation NVARCHAR(MAX) AS JSON
    ,DealerPrefLocation NVARCHAR(MAX) AS JSON) A
OUTER APPLY OPENJSON(A.DealerInformation)
WITH(Altername NVARCHAR(MAX) AS JSON
    ,DealerType NVARCHAR(MAX) AS JSON
    ,firstName NVARCHAR(MAX)
    ,DealerType NVARCHAR(MAX) AS JSON
    ,middleName NVARCHAR(MAX)
    ,lastName NVARCHAR(MAX)
    ,primaryDealerState NVARCHAR(MAX)
    ,otherDealerState NVARCHAR(MAX) AS JSON) B
OUTER APPLY OPENJSON(A.DealerPrefLocation) 
WITH(PrefLocation NVARCHAR(MAX) AS JSON
    ,NonPrefLocation NVARCHAR(MAX) AS JSON) C

更新从表中选择

尝试这个

SELECT B.firstName
      ,B.middleName
      ,B.lastName
      ,JSON_VALUE(B.DealerType,'$.id') AS DealerTypeId
      ,B.PrimaryDealerState
      ,B.otherDealerState                                          --You can dive deeper to parse that array
      ,JSON_VALUE(B.Altername,'$[0].firstName') AS Alter_firstName --there might be more...
      ,JSON_VALUE(C.PrefLocation,'$[0].address.address1') AS pref_address --there might be more...
      ,JSON_VALUE(C.PrefLocation,'$[0].apptPhoneNumber') AS pref_apptPhoneNumber
      ,JSON_VALUE(C.NonPrefLocation,'$[0].address.address1') AS nonpref_address --there might be more...
      ,JSON_VALUE(C.NonPrefLocation,'$[0].ServiceName') AS nonpref_ServiceName
FROM Test_JSON_File 
CROSS APPLY OPENJSON(value_id)
WITH(DealerInformation NVARCHAR(MAX) AS JSON
    ,DealerPrefLocation NVARCHAR(MAX) AS JSON) A
OUTER APPLY OPENJSON(A.DealerInformation)
WITH(Altername NVARCHAR(MAX) AS JSON
    ,DealerType NVARCHAR(MAX) AS JSON
    ,firstName NVARCHAR(MAX)
    ,DealerType NVARCHAR(MAX) AS JSON
    ,middleName NVARCHAR(MAX)
    ,lastName NVARCHAR(MAX)
    ,primaryDealerState NVARCHAR(MAX)
    ,otherDealerState NVARCHAR(MAX) AS JSON) B
OUTER APPLY OPENJSON(A.DealerPrefLocation) 
WITH(PrefLocation NVARCHAR(MAX) AS JSON
    ,NonPrefLocation NVARCHAR(MAX) AS JSON) C;
于 2018-10-09T06:42:47.303 回答