如何在子 OPENJSON 的结果上使用 OPENJSON

How do I use OPENJSON on the result of a sub OPENJSON

我有这样的 JSON 结构:

Declare @layout NVARCHAR(MAX) = N'
    {
        "Sections": [
            {
                "SectionName":"Section1",
                "SectionOrder":1,
                "Fields":[
                    {
                        "FieldName":"Field1",
                        "FieldData":"Data1"
                    },
                    {
                        "FieldName":"Field2",
                        "FieldData":"Data2"
                    }
                ]
            },
            {
                "SectionName":"Section2",
                "SectionOrder":2,
                "Fields":[
                    {
                        "FieldName":"Field3",
                        "FieldData":"Data3"
                    },
                    {
                        "FieldName":"Field4",
                        "FieldData":"Data4"
                    }
                ]
            }
        ]
    }
'

如何查询到 Sections.Fields.FieldName = 'Field3' 的等价物?

select *
from OPENJSON(@layout,'$.Sections') 
WITH (
    SectionName nvarchar(MAX) '$.SectionName',  
    SectionOrder nvarchar(MAX) '$.SectionOrder', 
    Fields nvarchar(MAX) '$.Fields' as JSON
)

这是我得到的,我不能再往下看了,否则不会return任何结果。

阅读文档后想通了,完全有效!

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