无法使用 OPENJSON 解析字符串
Posted
技术标签:
【中文标题】无法使用 OPENJSON 解析字符串【英文标题】:Unable to parse string using OPENJSON 【发布时间】:2019-06-03 06:54:11 【问题描述】:我对 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'
)
【问题讨论】:
请说明错误或您在使用 OPENJSON 时遇到的实际问题。 如果没有样本数据和您的尝试,恐怕我们无法为您提供帮助。我们有您预期的结果,但仅此而已。向我们展示您的尝试,并解释为什么它不起作用。 对不起-我正在尝试以下代码(我上面发布的json是来自的数据 【参考方案1】:您需要做的是将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
【讨论】:
效果很好。非常感谢 - 但我如何也包括休息时间 - 可能有多个?以上是关于无法使用 OPENJSON 解析字符串的主要内容,如果未能解决你的问题,请参考以下文章