创建嵌套的空Json对象
我想创建嵌套的空Json对象。我尝试使用创建嵌套的空Json对象,json,sql-server,tsql,sql-server-2019,Json,Sql Server,Tsql,Sql Server 2019,我想创建嵌套的空Json对象。我尝试使用JSON\u查询('[]')创建一个层。但是如何处理嵌套对象呢 预期产出应为: { "Model": [ { "ModelName": "Registration", "Student": [ { "Marks": [
JSON\u查询('[]')
创建一个层。但是如何处理嵌套对象呢
预期产出应为:
{
"Model": [
{
"ModelName": "Registration",
"Student": [
{
"Marks": [
{
"Notes": [
{
"Visible": []
}
]
}
]
}
]
}
]
}
为了实现嵌套json,可以使用连接。一种可能的解决方案是定义表变量,然后将它们连接起来以构建所需的结构。 然后,您可以使用
为json auto
生成json输出
以下是一个简单的查询,您可以从中开始:
declare @Model table (ModelName nvarchar(50))
declare @Student table (ModelName nvarchar(50),Student nvarchar(50))
declare @Marks table (Student nvarchar(50),Mark nvarchar(50))
declare @Notes table (Mark nvarchar(50))
declare @Visible table (Mark nvarchar(50))
insert into @Model select 'Registration'
select *
from
@model Model
left join @Student Student on Model.ModelName= Student.ModelName
left join @Marks Marks on Marks.Student = Student.Student
left join @Notes Notes on Notes.Mark = Marks.Mark
left join @Visible Visible on Visible.Mark = Marks.Mark
for json auto, ROOT('Model')
输出:
{
"Model":[
{
"ModelName":"Registration",
"Student":[
{
"Marks":[
{
"Notes":[
{
"Visible":[
{
}
]
}
]
}
]
}
]
}
]
}