无法使用动态部分格式化 JSON 导出

问题描述 投票:0回答:1

我需要帮助来构建可生成以下 JSON 结构的 TSQL 查询 (SQL Server 2016)。

结构如下,根称为“协议”。

每个协议都有主要属性AgreementDate、Position、OrganizationName。接下来是两个部分“OrganizationDetails”和“OrganizationDetails2”。所有这些都是所有协议的共同数据。

它的结尾部分可能因协议而异。在第一个协议(下面的示例)中,其称为“AgreementType1”,在第二个协议中,其称为“AgreementOfADifferentType”。最后一部分可以有不同的列设置,因为进入此部分的数据存储在不同的表中。

目前所有常用数据都存储在一张表中。它包括从列名“Agreement”到“TypeOfOrganization2”的数据。最后一个动态部分将数据存储到不同的表中。 然而,这些不同的表始终可以连接到“公共表”。

如果不是针对 JSON 的最后一个动态部分,那么构建 TSQL 查询生成此查询不会有任何问题...请注意,动态部分可以具有不同数量的列、不同的名称等...

如有任何帮助,我们将不胜感激! :)


JSON 示例

{"Agreement": 
[
    {
        "AgreementDate": "2024-01-01",
        "Position": "123",
        "OrganisationName": "Hello",
        "OrganisationDetails": {
            "BusinessDate": "2024-01-01",
            "TypeOfOrganization": 1
        },
        "OrganisationsDetails2": {
            "BusinessDate2": "2024-01-01",
            "TypeOfOrganization2": null
        },
       ** "AgreementType1":** {
            "DateOfRegistration": null,
            "Amount": 884400.00
        }
    },
    {
        "AgreementDate": "2024-01-01",
        "Position": "222",
        "OrganisationName": "Hello2222",
        "OrganisationDetails": {
            "BusinessDate": "2024-02-02",
            "TypeOfOrganization": 2
        },
        "OrganisationsDetails2": {
            "BusinessDate2": "2024-05-01",
            "TypeOfOrganization2": 5
        },
        **"AgreementOfADifferentType":** {
            "Date": "2024-22-01",
            "Date2": null,
            "AmountOfSales": null,
            "AmountOfSales2": 222.22
            
        }
    }
]

}

我已经尝试过 FOR JSON PATH 但无法让它工作。我也尝试过动态 SQL 尝试拼凑但没有成功。

json t-sql ssis
1个回答
0
投票

我打字速度很快,所以可能不完全符合你的预期,但关键是你必须提供给 JSON 的 PATRH

declare @Agreement Table (AgreementDate Date, Position int, OrganisationName nvarchar(50) )
insert into @Agreement Values (GetDate(), 123, 'Hello'),(GetDate(), 222, 'Hello2222')

declare @OrganisationDetails table (BusinessDate2 date, TypeOfOrganization int)
insert into @OrganisationDetails Values(GetDate(), 1), (GetDate(), 2), (GetDate(), 3)

declare @AgreementOfADifferentType table (ID int not null identity (1,1), [Date] Date, Date2 Date, AmountOfSales decimal null, AmountOfSales2 decimal)
insert into @AgreementOfADifferentType Values(GetDate(), null, null, 22.34)

select 
      [Aggreement.AgreementDate] = AgreementDate
    , [Aggreement.Position] = Position
    , [Aggreement.OrganisationName] = OrganisationName
    , [Aggreement.OrganisationDetails.BusinessDate2] = OrganisationDetails.BusinessDate2
    , [Aggreement.OrganisationDetails.TypeOfOrganization] = OrganisationDetails.TypeOfOrganization
    , [Aggreement.OrganisationDetails2.BusinessDate2] = OrganisationDetails2.BusinessDate2
    , [Aggreement.OrganisationDetails2.TypeOfOrganization] = OrganisationDetails2.TypeOfOrganization
    , [Aggreement.AgreementOfADifferentType.Date] = AgreementOfADifferentType.Date
    , [Aggreement.AgreementOfADifferentType.Date2] = AgreementOfADifferentType.Date2
    , [Aggreement.AgreementOfADifferentType.AmountOfSales] = AgreementOfADifferentType.AmountOfSales
    , [Aggreement.AgreementOfADifferentType.AmountOfSales2] = AgreementOfADifferentType.AmountOfSales2
from @Agreement Agreement
Left Join @OrganisationDetails OrganisationDetails on OrganisationDetails.BusinessDate2 = Agreement.AgreementDate and OrganisationDetails.TypeOfOrganization = 1
Left Join @OrganisationDetails OrganisationDetails2 on OrganisationDetails2.BusinessDate2 = Agreement.AgreementDate and OrganisationDetails2.TypeOfOrganization = 2
Left Join @AgreementOfADifferentType AgreementOfADifferentType on  AgreementOfADifferentType.[Date] = Agreement.AgreementDate
FOR JSON PATH

输出将是

[
    {
        "Aggreement": {
            "AgreementDate": "2024-04-29",
            "Position": 123,
            "OrganisationName": "Hello",
            "OrganisationDetails": {
                "BusinessDate2": "2024-04-29",
                "TypeOfOrganization": 1
            },
            "OrganisationDetails2": {
                "BusinessDate2": "2024-04-29",
                "TypeOfOrganization": 2
            },
            "AgreementOfADifferentType": {
                "Date": "2024-04-29",
                "AmountOfSales2": 22
            }
        }
    },
    {
        "Aggreement": {
            "AgreementDate": "2024-04-29",
            "Position": 222,
            "OrganisationName": "Hello2222",
            "OrganisationDetails": {
                "BusinessDate2": "2024-04-29",
                "TypeOfOrganization": 1
            },
            "OrganisationDetails2": {
                "BusinessDate2": "2024-04-29",
                "TypeOfOrganization": 2
            },
            "AgreementOfADifferentType": {
                "Date": "2024-04-29",
                "AmountOfSales2": 22
            }
        }
    }
]
© www.soinside.com 2019 - 2024. All rights reserved.