IN ADF Data Flow how can I flatten JSON into rows rather than columns?
{
"header": [
{
"main": {
"id": 1
},
"sub": [
{
"type": "a",
"id": 2
},
{
"type": "b",
"id": 3
}
]}]}
In ADF I'm using the flatten task and get the below result:
However the result I'm trying to achieve is merging the two id columns into one column like below:
CodePudding user response:
Since both main_id and sub_id belong in the same column, instead of using 1 flatten to flatten all the data, flatten both main and sub separately.
- I have taken the following JSON as source for my dataflow.
{
"header":[
{
"main":{
"id":1
},
"sub":[
{
"type":"a",
"id":2
},
{
"type":"b",
"id":3
}
]
},
{
"main":{
"id":4
},
"sub":[
{
"type":"c",
"id":5
},
{
"type":"d",
"id":6
}
]
}
]
}
- I have taken 2 flatten transformations
flattenMainandflattenSubinstead of 1 which use the same source. - For
flattenMain, I have unrolled byheaderand selected unroll root asheader. Then created an additional column selecting source columnheader.main.id.

- The data preview for
flattenMainwould be:

- For
flattenSub, I have unrolled byheader.suband selected unroll root asheader.sub. Then created 2 additional column selecting source columnheader.sub.idas id column andheader.sub.typeas type column.

- The data preview for
flattenSubtransformation would be:

- Now I have applied
uniontransformation on bothflattenMainandflattenSub. I have appliedunion byusing Name.

- The final data preview for this Union transformation will give the desired result.

NOTE: All the highlighted rows in output images indicate the result that would be achieved when we use the JSON sample provided in the question.


