我有一个嵌套的 JSON 作为表单的输出,我需要解析它以便将它发送到连接表中。我无法解析 JSON 中的一些数据:标签、范围值或正文 我得到 NULL 有人遇到过这种类型吗?
DECLARE @json NVARCHAR(MAX)
SET @json = '{
"Id":"712db489",
"label":"kjk",
"ranges":{
"rangeQuestion":null,
"minRange":0,
"maxRange":10,
"rangeValues":[1,2],
"hasMarks":false
},
"labels":[1,2],
"options":[
{
"body":"Yes",
"sequence":1
},
{
"body":"No",
"sequence":2
}
]
}'
SELECT * FROM
OPENJSON ( @json )
WITH (
label nvarchar(250), --ok
maxRange nvarchar(250) '$.ranges.maxRange', --ok
labels nvarchar(250), -- not parsed
rangesValues nvarchar(250) '$.ranges.rangeValues' , -- not parsed
body nvarchar(250) '$.options.body' -- not parsed
)
输出应该像
minRange maxRange rangeValues
0 10 1
0 10 2
身体序列
是的 1
2号
如果嵌套了 3 个级别,可以做什么?
DECLARE @json NVARCHAR(MAX)
SET @json = '{
"Id":"712db489",
"label":"kjk",
"ranges":{
"rangeQuestion":null,
"minRange":0,
"maxRange":10,
"rangeValues": [
{
"rangeValue": 1,
"otherValue": 10
},
{
"rangeValue": 2,
"otherValue": 20
}
],
"hasMarks":false
},
"labels":[1,2],
"options":[
{
"body":"Yes",
"sequence":1
},
{
"body":"No",
"sequence":2
}
]
}'
SELECT r.minRange,
r.maxRange,
rV.[value] AS rangeValue
FROM OPENJSON (@json, '$.ranges')
WITH (minRange int,
maxRange int,
rangeValues nvarchar(MAX)
AS JSON) r
CROSS APPLY OPENJSON (r.rangeValues) rV;