我有以下JSON数据,我试图使用openjson和交叉应用来检索数据。
[
[
{
"Staff": {
"StaffKey": "abcdefg-6136-478f-abcdefg-09db9d374b0a",
"Id": "123456",
"FirstName": "John",
"LastName": "Doe",
"Email": "",
"Tags": null
},
"Location": {
"CompanyKey": "abcdefg-f99d-465a-abcdefg-fc8511058bbf",
"LocationKey": 987654,
"Id": "99",
"Name": "Acme, Inc.",
"Address": "123 Main St CityUSA NY 12345",
"Abbrev": "Acme, Inc.",
"Notes": null,
"TimeZone": null,
"Tags": null
},
"IsCredentialed": false,
"InactiveDate": null,
"Credentials": [],
"Tags": null,
"ExtendedCredentialingFields": {
"LocationIds": [
"65"
],
"CompanyKey": "abcdefg-32b8-42cf-abcdefg-5d5e96fbc43c",
"LocationDetails": {
"Name": "Acme, Inc.",
"CorporateEntity": "ACME",
"AddressLine1": "123 Main St",
"AddressLine2": null,
"City": "CityUSA",
"State": "NY",
"Zip": "12345",
"PhoneNumber": null,
"Ext": null,
"Fax": null
},
"StaffMemberMappingId": "456789",
"PlacementDate": null,
"LocationEmploymentType": null,
"ContractStart": null,
"ContractedHours": null,
"ExpectedStartDate": null,
"FirstShift": null,
"LastShift": null,
"LocationPreference": null,
"SeesPatients": true
}
}
]
]
使用以下SQL查询提供空值
Declare @JSON varchar(max)
SELECT @JSON=BulkColumn
FROM OPENROWSET (BULK 'D:\pathtojsonfile\staff_location_test_data.json', SINGLE_CLOB) import
select
Staff_level1.StaffKey, Staff_level1.Id
from openjson (@JSON)
with
(
StaffKey nvarchar(100),
Id nvarchar(25)
--[Staff] nvarchar(Max) as json
)
as Staff_level1
想知道我能不能帮你
1.“StaffKey”,“Id”,&“LastName”from Staff
1.“CompanyKey”,“Id”&“Address”from Location
- ExtendedCredentialingFields中的“Name”、“Address”、“City”、“State”和“Zip”
我尝试使用交叉应用,但没有得到任何结果
Declare @JSON varchar(max)
SELECT @JSON=BulkColumn
FROM OPENROWSET (BULK 'D:\pathtojsonfile\staff_location_test_data.json', SINGLE_CLOB) import
select
Staff_level2.StaffKey, Staff_level2.Id
from openjson (@JSON)
with
(
[Staff] nvarchar(Max) as json
)
as Staff_level1
cross apply openjson (Staff_level1.Staff)
with
(
StaffKey nvarchar(100),
Id nvarchar(25)
) as Staff_level2
我只是不太熟悉这种JSON格式,无法提取我正在寻找的元素。
1条答案
按热度按时间ufj5ltwl1#
问题中编写的查询仅适用于简单的非嵌套JSON数据。
为了准确地提取和管理复杂的多级嵌套JSON,我们应该使用CROSS APPLY来使用嵌套数据结构。这允许我们从JSON中的不同部分检索值,精确地定位所需的嵌套数据
试试这个
Query2
演示https://dbfiddle.uk/-viKFaQf