如何使用 openjson 递归解析 JSON 字符串
How to parse JSON string recursively with openjson
我有以下 JSON 数据:
set @json = N'{
"Book":{
"IssueDate":"02-15-2019"
, "Detail":{
"Type":"Any Type"
, "Author":{
"Name":"Annie"
, "Sex":"Female"
}
}
, "Chapter":[
{
"Section":"1.1"
, "Title":"Hello world."
}
,
{
"Section":"1.2"
, "Title":"Be happy."
}
]
, "Sponsor":["A","B","C"]
}
}'
预期的结果是
topKey Key Value
Book IssueDate 02-15-2019
Book Detail { "Type":"Any Type", "Author":{ "Name":"Annie" , "Sex":"Female"}
Book Chapter [{ "Section":"1.1", "Title":"Hello world." }, { "Section":"1.2", "Title":"Be happy." }]
Book Sponsor ["A","B","C"]
Detail Type Any Type
Detail Author { "Name":"Annie" ,"Sex":"Female"}
Author Name Annie
Author Sex Female
Chapter Section 1.1
Chapter Title Hello world
Chapter Section 1.2
Chapter Title Be happy.
我发现当字段"Value"为JSON时,我需要继续解析它
所以我创建了一个函数来做解析工作,但是它 returns ''
不符合要求。
create function ParseJson(@json nvarchar(max))
returns @tempTable table ([key] nvarchar(max), [value] nvarchar(max))
as
begin
insert @tempTable
select
x.[key]
, x.[value]
from
openjson(@json) x
cross apply ParseJson(x.[value]) y
where ISJSON(x.[value])=1
end
可以将字符串传递给函数。
select * from ParseJson(@json)
我不确定您对结果的期望是否合理,但显然函数返回的 table 与您所说的不符——它缺少 topKey
列。出于这个原因,我宁愿聚合层次结构的路径。开始吧:
create function ParseJson(
@parent nvarchar(max), @json nvarchar(max))
returns @tempTable table (
[key] nvarchar(max), [value] nvarchar(max))
as
begin
; with cte as (
select
iif(@parent is null, [key]
, concat(@parent, '.', [key])) [key]
, [value]
from
openjson(@json)
)
insert
@tempTable
select
x.*
from
cte x
union all
select
x.*
from
cte y
cross apply ParseJson(y.[key], y.[value]) x
where isjson(y.[value])=1
return
end
结果:
我有以下 JSON 数据:
set @json = N'{
"Book":{
"IssueDate":"02-15-2019"
, "Detail":{
"Type":"Any Type"
, "Author":{
"Name":"Annie"
, "Sex":"Female"
}
}
, "Chapter":[
{
"Section":"1.1"
, "Title":"Hello world."
}
,
{
"Section":"1.2"
, "Title":"Be happy."
}
]
, "Sponsor":["A","B","C"]
}
}'
预期的结果是
topKey Key Value
Book IssueDate 02-15-2019
Book Detail { "Type":"Any Type", "Author":{ "Name":"Annie" , "Sex":"Female"}
Book Chapter [{ "Section":"1.1", "Title":"Hello world." }, { "Section":"1.2", "Title":"Be happy." }]
Book Sponsor ["A","B","C"]
Detail Type Any Type
Detail Author { "Name":"Annie" ,"Sex":"Female"}
Author Name Annie
Author Sex Female
Chapter Section 1.1
Chapter Title Hello world
Chapter Section 1.2
Chapter Title Be happy.
我发现当字段"Value"为JSON时,我需要继续解析它
所以我创建了一个函数来做解析工作,但是它 returns ''
不符合要求。
create function ParseJson(@json nvarchar(max))
returns @tempTable table ([key] nvarchar(max), [value] nvarchar(max))
as
begin
insert @tempTable
select
x.[key]
, x.[value]
from
openjson(@json) x
cross apply ParseJson(x.[value]) y
where ISJSON(x.[value])=1
end
可以将字符串传递给函数。
select * from ParseJson(@json)
我不确定您对结果的期望是否合理,但显然函数返回的 table 与您所说的不符——它缺少 topKey
列。出于这个原因,我宁愿聚合层次结构的路径。开始吧:
create function ParseJson(
@parent nvarchar(max), @json nvarchar(max))
returns @tempTable table (
[key] nvarchar(max), [value] nvarchar(max))
as
begin
; with cte as (
select
iif(@parent is null, [key]
, concat(@parent, '.', [key])) [key]
, [value]
from
openjson(@json)
)
insert
@tempTable
select
x.*
from
cte x
union all
select
x.*
from
cte y
cross apply ParseJson(y.[key], y.[value]) x
where isjson(y.[value])=1
return
end
结果: