简体   繁体   中英

Azure Data Factory - SQL to nested JSON

I have a SQL database with tables for Staff and Appointments (1 staff : many appointments). I'd like to use Azure Data Factory to output this to nested JSON in a Blob store in a format similar to the below:

[
   {
      "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"
         }
      ]
   }
]

I've tried using the Copy activity but this produces flat JSON structures rather than the nested structure described above. Has anyone got a way to do this please?

More scenarios for JSON data in ADF is flattening. However,according to your description,your need producing JSON contains Json array group by some columns.Something like merge appointment things by same staff .

If my understanding is right,then you could get some clues from my previous case: How to split into Sub Documents with Nesting separator? . Please refer to my test:

Simulate your sample data:

在此处输入图片说明

Use sql in sql db source dataset:

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;

在此处输入图片说明

Output in blob storage:

在此处输入图片说明

I try to verify the json format and it is correct.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM