I've had created the function ParseJson
below :
create function ParseJson(
@json nvarchar(max))
returns @tempTable table (topKey nvarchar(max), [Key] nvarchar(max), [Value] nvarchar(max))
as
begin
insert
@tempTable
select
x.[key] topKey
, y.[key]
, y.[value]
from openjson(@json) x
cross apply openjson(x.[value]) y
return
end
-- execute
select * from ParseJson(@json)
I defined a variable @json
which is not an array string :
set @json =
N'{
"Chapter":
{
"Section":"1.1"
, "Title":"Hello world."
}
}'
Then I execute the query I will get the result :
If I reset the variable @json
which is an array string :
declare @json nvarchar(max)
set @json =
N'{
"Chapter":[
{
"Section":"1.1"
, "Title":"Hello world."
}
,
{
"Section":"1.2"
, "Title":"Be happy."
}
]
}'
Then I execute the query I will get the result :
I hope I can get the result below :
I don't know the result that I expectation is reasonable?
How can I adjust the function to meet the expectation?