Azure数据工厂-嵌套JSON的SQL

时间:2020-03-30 15:20:07

标签: sql json azure azure-data-factory

我有一个SQL数据库,其中包含用于职员和任命的表(1名职员:很多任命)。我想使用Azure数据工厂以类似于以下格式将其输出到Blob存储区中的嵌套JSON:

[
   {
      "staffid":"101",
      "firstname":"Donald",
      "lastname":"Duck",
      "appointments":[
         {
            "appointmentid":"201",
            "startdate":"2020-02-01T00:00:00",
            "enddate":"2020-04-29T23:00:00"
         },
         {
            "appointmentid":"202",
            "startdate":"2020-01-01T00:00:00",
            "enddate":"2020-01-31T00:00:00"
         }
      ]
   },
   {
      "staffid":"102",
      "firstname":"Mickey",
      "lastname":"Mouse",
      "appointments":[
         {
            "appointmentid":"203",
            "startdate":"2020-02-01T00:00:00",
            "enddate":"2020-04-29T23:00:00"
         },
         {
            "appointmentid":"204",
            "startdate":"2020-01-01T00:00:00",
            "enddate":"2020-01-31T00:00:00"
         }
      ]
   }
]

我尝试使用Copy活动,但这会生成平面JSON结构,而不是上述的嵌套结构。有没有人有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

ADF中JSON数据的更多方案正在趋于平缓。但是,根据您的描述,您需要生成的JSON包含按列划分的Json数组组。诸如通过同一appointment things合并staff的事情。

如果我的理解正确,那么您可以从我以前的案例中获得一些线索:How to split into Sub Documents with Nesting separator?。请参考我的测试:

模拟您的样本数据:

enter image description here

在sql db源数据集中使用sql:

select app.staffid,app.firstname,app.lastname,
'appointments' = (
            SELECT
                appointmentid AS 'appointmentid',startdate as 'startdate',enddate as 'enddate'
            FROM
                dbo.appoint as app2
            where app2.staffid = app.staffid and
            app2.firstname = app.firstname and
            app2.lastname = app.lastname
            FOR JSON PATH)
from dbo.appoint as app
group by app.staffid,app.firstname,app.lastname
FOR JSON Path;

enter image description here

blob存储中的输出:

enter image description here

我尝试验证json格式,并且格式正确。

enter image description here