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
flattenMain
andflattenSub
instead of 1 which use the same source. - For
flattenMain
, I have unrolled byheader
and selected unroll root asheader
. Then created an additional column selecting source columnheader.main.id
.
- The data preview for
flattenMain
would be:
- For
flattenSub
, I have unrolled byheader.sub
and selected unroll root asheader.sub
. Then created 2 additional column selecting source columnheader.sub.id
as id column andheader.sub.type
as type column.
- The data preview for
flattenSub
transformation would be:
- Now I have applied
union
transformation on bothflattenMain
andflattenSub
. I have appliedunion by
using 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.