如何使用具有 undetermined/dynamic 路径的嵌套 JSON 的 OPENJSON 方法?
How to use OPENJSON Method with nested JSON having undetermined/dynamic path?
我正在使用一个 API 以动态 json 的形式吐出数据,即 json 对象的某些元素的位置不固定,这导致json 数组中的一些元素是动态的。例如在“属性”数组下面的JSON对象中,可以属于n级嵌套。 OPENJSON 是否有任何 search/lookup 功能可以找到名称为“属性”的数组的位置并将元素打印到 table 中?
这是工作模型,但我必须对数组使用强制索引 ('$.rows[2]') 才能显示所需的结果。但是 属性 数组可以在任何地方。
JSON
Declare @Jsonobj as nvarchar(max)
Select @Jsonobj = N'{
"ID": "StudentInformation",
"Name": "Student Information",
"Type": "s_info",
"Details": [
"Student Information",
"Greendale Community College"
],
"Date": "21 October 2021",
"Rows": [
{
"RowType": "Header",
"Cells": [
{
"Value": ""
},
{
"Value": "21 Feb 2021"
},
{
"Value": "22 Aug 2020"
}
]
},
{
"RowType": "Section",
"Title": "Class",
"Rows": []
},
{
"RowType": "Section",
"Title": "Grade",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "5A",
"Property": [
{
"Id": "1",
"Value": "John Smith"
}
]
},
{
"Value": "5A",
"Property": [
{
"Id": "2",
"Value": "Jane Doe"
}
]
},
{
"Value": "5B",
"Property": [
{
"Id": "1",
"Value": "Ben Frank"
}
]
}
]
}
]
}
]
}'
SQL
SELECT JSON_VALUE(v.value, 'strict $.Value') as Names
FROM OPENJSON(@Jsonobj, 'strict $.Rows[2].Rows') c
CROSS APPLY OPENJSON(c.value, 'strict $.Cells') p
CROSS APPLY OPENJSON(p.value, 'strict $.Property') v
这是我发现有效的方法:
SELECT d.value Names
FROM OPENJSON(@Jsonobj,'$.Rows')
WITH([Rows] NVARCHAR(MAX) AS JSON) A
OUTER APPLY OPENJSON(A.[Rows]) B
OUTER APPLY OPENJSON(B.[value],'$.Cells') C
OUTER APPLY OPENJSON(c.[value],'$.Property') WITH (Value NVARCHAR(MAX)) d
where d.value is not null
我正在使用一个 API 以动态 json 的形式吐出数据,即 json 对象的某些元素的位置不固定,这导致json 数组中的一些元素是动态的。例如在“属性”数组下面的JSON对象中,可以属于n级嵌套。 OPENJSON 是否有任何 search/lookup 功能可以找到名称为“属性”的数组的位置并将元素打印到 table 中?
这是工作模型,但我必须对数组使用强制索引 ('$.rows[2]') 才能显示所需的结果。但是 属性 数组可以在任何地方。
JSON
Declare @Jsonobj as nvarchar(max)
Select @Jsonobj = N'{
"ID": "StudentInformation",
"Name": "Student Information",
"Type": "s_info",
"Details": [
"Student Information",
"Greendale Community College"
],
"Date": "21 October 2021",
"Rows": [
{
"RowType": "Header",
"Cells": [
{
"Value": ""
},
{
"Value": "21 Feb 2021"
},
{
"Value": "22 Aug 2020"
}
]
},
{
"RowType": "Section",
"Title": "Class",
"Rows": []
},
{
"RowType": "Section",
"Title": "Grade",
"Rows": [
{
"RowType": "Row",
"Cells": [
{
"Value": "5A",
"Property": [
{
"Id": "1",
"Value": "John Smith"
}
]
},
{
"Value": "5A",
"Property": [
{
"Id": "2",
"Value": "Jane Doe"
}
]
},
{
"Value": "5B",
"Property": [
{
"Id": "1",
"Value": "Ben Frank"
}
]
}
]
}
]
}
]
}'
SQL
SELECT JSON_VALUE(v.value, 'strict $.Value') as Names
FROM OPENJSON(@Jsonobj, 'strict $.Rows[2].Rows') c
CROSS APPLY OPENJSON(c.value, 'strict $.Cells') p
CROSS APPLY OPENJSON(p.value, 'strict $.Property') v
这是我发现有效的方法:
SELECT d.value Names
FROM OPENJSON(@Jsonobj,'$.Rows')
WITH([Rows] NVARCHAR(MAX) AS JSON) A
OUTER APPLY OPENJSON(A.[Rows]) B
OUTER APPLY OPENJSON(B.[value],'$.Cells') C
OUTER APPLY OPENJSON(c.[value],'$.Property') WITH (Value NVARCHAR(MAX)) d
where d.value is not null