我对OPENJSON经验不足,并且尝试使用示例,但无法解析下面的字符串
任何人都可以帮助我。
我希望它采用以下格式:
Date,TimeSheet.StartTime,TimeSheet.FinishTime,Timesheet.BreakStart,TimeSheet.BreakFinish,Timesheet.EmployeeId,TimeSheet.Notes
[
{
"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..."
}
]
}
]
这是我要使用的代码:
DECLARE @json NVARCHAR(MAX)
DECLARE @json2 NVARCHAR(MAX)
SET @json= (Select [TimeSheetTestData] FROM [TimeSheetTestData] WHERE [TimeSheetTestDataId] = 3) -- this is the sample json above
SET @json2=(SELECT Value
FROM OpenJson(@json) where [Key] = 'TimeSheet')
--SELECT [Key], Value
--FROM OpenJson(@json2)
SELECT * FROM
OPENJSON ( @json2 )
WITH (
startTime varchar(200) '$.StartTime' ,
finishTime varchar(200) '$.FinishTime' ,
breakTimes varchar(200) '$.BreakTimes' ,
employeeId varchar(200) '$.EmployeeId' ,
notes varchar(200) '$.Notes'
)
答案 0 :(得分:0)
您需要做的是将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