我有一些json,我想在SQL Server 2016中解析。有一個Projects-> Structures-> Properties的層次結構。我想編寫解析整個層次的查詢,但我不希望指定索引號的任何元素,即我不想做這樣的事:SQL Server OPENJSON讀取嵌套的json
openjson (@json, '$[0]')
或
openjson (@json, '$.structures[0]')
我有這個想法,我可以讀取頂級項目對象的值以及表示它下面的結構的json字符串,然後可以單獨解析這些結構。問題是,下面的代碼無法正常工作:
declare @json nvarchar(max)
set @json = '
[
{
"IdProject":"97A76363-095D-4FAB-940E-9ED2722DBC47",
"Name":"Test Project",
"structures":[
{
"IdStructure":"CB0466F9-662F-412B-956A-7D164B5D358F",
"IdProject":"97A76363-095D-4FAB-940E-9ED2722DBC47",
"Name":"Test Structure",
"BaseStructure":"Base Structure",
"DatabaseSchema":"dbo",
"properties":[
{
"IdProperty":"618DC40B-4D04-4BF8-B1E6-12E13DDE86F4",
"IdStructure":"CB0466F9-662F-412B-956A-7D164B5D358F",
"Name":"Test Property 2",
"DataType":1,
"Precision":0,
"Scale":0,
"IsNullable":false,
"ObjectName":"Test Object",
"DefaultType":1,
"DefaultValue":""
},
{
"IdProperty":"FFF433EC-0BB5-41CD-8A71-B5F09B97C5FC",
"IdStructure":"CB0466F9-662F-412B-956A-7D164B5D358F",
"Name":"Test Property 1",
"DataType":1,
"Precision":0,
"Scale":0,
"IsNullable":false,
"ObjectName":"Test Object",
"DefaultType":1,
"DefaultValue":""
}
]
}
]
}
]';
select IdProject, Name, structures
from openjson (@json)
with
(
IdProject uniqueidentifier,
Name nvarchar(100),
structures nvarchar(max)
) as Projects
IdProject和名稱得到恢復沒有問題,但由於某種原因,我不能在「結構」舉行的嵌套JSON。取而代之的是JSON內容只是它返回NULL:
有誰知道這是可能的,如果是的話,我究竟做錯了什麼?