2016-11-07 60 views
2

我有一個JSON結構,其中有部分,由多個渲染,它由多個字段組成。你怎麼打開陣列的數組

如何在最低級別(字段)上執行1個OPENJSON調用以從中獲取所有信息?

下面是一個例子JSON:

Declare @layout NVARCHAR(MAX) = N' 
    { 
     "Sections": [ 
      { 
       "SectionName":"Section1", 
       "SectionOrder":1, 
       "Renders":[ 
        { 
         "RenderName":"Render1", 
         "RenderOrder":1, 
         "Fields":[ 
          { 
           "FieldName":"Field1", 
           "FieldData":"Data1" 
          }, 
          { 
           "FieldName":"Field2", 
           "FieldData":"Data2" 
          } 
         ] 
        }, 
        { 
         "RenderName":"Render2", 
         "RenderOrder":2, 
         "Fields":[ 
          { 
           "FieldName":"Field1", 
           "FieldData":"Data1" 
          }, 
          { 
           "FieldName":"Field2", 
           "FieldData":"Data2" 
          } 
         ] 
        } 
       ] 
      }, 
      { 
       "SectionName":"Section2", 
       "SectionOrder":2, 
       "Renders":[ 
        { 
         "RenderName":"Render1", 
         "RenderOrder":1, 
         "Fields":[ 
          { 
           "FieldName":"Field1", 
           "FieldData":"Data1" 
          } 
         ] 
        }, 
        { 
         "RenderName":"Render2", 
         "RenderOrder":2, 
         "Fields":[ 
          { 
           "FieldName":"Field1", 
           "FieldData":"Data1" 
          }, 
          { 
           "FieldName":"Field2", 
           "FieldData":"Data2" 
          } 
         ] 
        } 
       ] 
      } 
     ] 
    } 
' 

下面是一個嵌套OPENJSON電話,其工作方式的代碼的例子,但很複雜,不能動態地生成,我該如何讓一個級別的呼叫?

SELECT SectionName, SectionOrder, RenderName, RenderOrder, FieldName, FieldData FROM (
    SELECT SectionName, SectionOrder, RenderName, RenderOrder, Fields FROM (
     select SectionName, SectionOrder, Renders 
     from OPENJSON(@layout,'$.Sections') 
     WITH (
      SectionName nvarchar(MAX) '$.SectionName', 
      SectionOrder nvarchar(MAX) '$.SectionOrder', 
      Renders nvarchar(MAX) '$.Renders' as JSON 
     ) 
    ) as Sections 
    CROSS APPLY OPENJSON(Renders,'$') 
    WITH (
     RenderName nvarchar(MAX) '$.RenderName', 
     RenderOrder nvarchar(MAX) '$.RenderOrder', 
     Fields nvarchar(MAX) '$.Fields' as JSON 
    ) 
) as Renders 
CROSS APPLY OPENJSON(Fields,'$') 
WITH (
    FieldName nvarchar(MAX) '$.FieldName', 
    FieldData nvarchar(MAX) '$.FieldData' 
) 

這是我想達到的目標:

select FieldName, FieldData 
from OPENJSON(@layout,'$.Sections.Renders.Fields') 
WITH (
    FieldName nvarchar(MAX) '$.Sections.Renders.Fields.FieldName', 
    FieldData nvarchar(MAX) '$.Sections.Renders.Fields.FieldData' 
) 
+0

我認爲你必須保持'CROSS APPLY'ing - 也就是說,我不知道你可以在一個'OPENJSON'中。 [這個答案能讓你得到你需要的東西](https://stackoverflow.com/a/372​​18450/1028230)? – ruffin

回答

0

雖然你不能逃脫只使用單一OPENJSON,可以簡化您的查詢一下,以便更容易地創建動態刪除嵌套的子查詢:

SELECT SectionName, SectionOrder, RenderName, RenderOrder, FieldName, FieldData 
FROM OPENJSON(@layout, '$.Sections') 
WITH (
    SectionName NVARCHAR(MAX) '$.SectionName', 
    SectionOrder NVARCHAR(MAX) '$.SectionOrder', 
    Renders NVARCHAR(MAX) '$.Renders' AS JSON 
) 
CROSS APPLY OPENJSON(Renders,'$') 
WITH (
    RenderName NVARCHAR(MAX) '$.RenderName', 
    RenderOrder NVARCHAR(MAX) '$.RenderOrder', 
    Fields NVARCHAR(MAX) '$.Fields' AS JSON 
) 
CROSS APPLY OPENJSON(Fields,'$') 
WITH (
    FieldName NVARCHAR(MAX) '$.FieldName', 
    FieldData NVARCHAR(MAX) '$.FieldData' 
)