Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建嵌套的空Json对象_Json_Sql Server_Tsql_Sql Server 2019 - Fatal编程技术网

创建嵌套的空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对象。我尝试使用
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":[
                              {

                              }
                           ]
                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}