相关疑难解决方法(0)

SQL Server OPENJSON读取嵌套的json

我想在SQL Server 2016中解析一些json.有一个Projects-> Structures-> Properties的层次结构.我想编写一个解析整个层次结构的查询,但我不想按索引号指定任何元素,即我不想做这样的事情:

openjson (@json, '$[0]')
Run Code Online (Sandbox Code Playgroud)

要么

openjson (@json, '$.structures[0]')
Run Code Online (Sandbox Code Playgroud)

我有这个想法,我可以读取顶级项目对象的值以及表示它下面的结构的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 …
Run Code Online (Sandbox Code Playgroud)

parsing json hierarchy sql-server-2016 open-json

28
推荐指数
3
解决办法
3万
查看次数

标签 统计

hierarchy ×1

json ×1

open-json ×1

parsing ×1

sql-server-2016 ×1