我有一些我想在 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 中缺少什么或者我需要添加什么?