Вам нужно использовать OPENJSON () с явной схемой и оператором CROSS APPLY
.
Ввод:
DECLARE @json nvarchar(max) = N'
[
{
"Date": "0001-01-01T00:00:00+00:00",
"TimeSheet": [
{
"StartTime": "0001-01-01T00:00:00+00:00",
"FinishTime": "0001-01-01T00:00:00+00:00",
"BreakTimes": [
"0001-01-01T00:00:00+00:00",
"0001-01-01T00:00:00+00:00"
],
"EmployeeId": 1,
"Notes": "Blah, blah, blah..."
}
]
},
{
"Date": "0001-01-01T00:00:00+00:00",
"TimeSheet": [
{
"StartTime": "0001-01-01T00:00:00+00:00",
"FinishTime": "0001-01-01T00:00:00+00:00",
"BreakTimes": [
"0001-01-01T00:00:00+00:00",
"0001-01-01T00:00:00+00:00"
],
"EmployeeId": 1,
"Notes": "Blah, blah, blah..."
}
]
}
]'
T-SQL:
SELECT
j1.[Date], j2.StartTime, j2.FinishTime, j2.BreakStart, j2.BreakFinish, j2.EmployeeId, j2.Notes
FROM OPENJSON(@json)
WITH (
[Date] nvarchar(25) '$.Date',
TimeSheet nvarchar(max) '$.TimeSheet' AS JSON
) j1
CROSS APPLY OPENJSON(j1.TimeSheet)
WITH (
StartTime nvarchar(25) '$.StartTime',
FinishTime nvarchar(25) '$.FinishTime',
BreakStart nvarchar(25) '$.BreakTimes[0]',
BreakFinish nvarchar(25) '$.BreakTimes[1]',
EmployeeId int '$.EmployeeId',
Notes nvarchar(max) '$.Notes'
) j2
Вывод:
Date StartTime FinishTime BreakStart BreakFinish EmployeeId Notes
0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 1 Blah, blah, blah...
0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 0001-01-01T00:00:00+00:00 1 Blah, blah, blah...
Обновление:
Если ваш JSON всегда с одним элементом в массиве "TimeSheet"
JSON, следующий оператор также является опцией:
SELECT
j.[Date],
j.StartTime,
j.FinishTime,
j.BreakStart,
j.BreakFinish,
j.EmployeeId,
j.Notes
FROM OPENJSON(@json)
WITH (
[Date] nvarchar(25) '$.Date',
StartTime nvarchar(25) '$.TimeSheet[0].StartTime',
FinishTime nvarchar(25) '$.TimeSheet[0].FinishTime',
BreakStart nvarchar(25) '$.TimeSheet[0].BreakTimes[0]',
BreakFinish nvarchar(25) '$.TimeSheet[0].BreakTimes[1]',
EmployeeId int '$.TimeSheet[0].EmployeeId',
Notes nvarchar(max) '$.TimeSheet[0].Notes'
) j