Reputation: 1
Can anyone let me know how can I transform this input json I have tried with below transformation but didn't worked.
I have tried to used filter function and groupBy function, but for Multiple output it is failing the testCase.
tried with this method `
%dw 2.0
output application/json
---
payload.Bd map (val,index) ->{
"d23": val,
"lt":(payload.output2 filter(payload.Bd contains val) map(Value) ->
{
Val34: Value.PId
}
)
}
input -
[
{
"Val34": "968",
"d23": "Y1"
},
{
"Val34": "958",
"d23": "Y2"
},
{
"Val34": "951",
"d23": "Y2"
}
]
expected output -
[
{
"d23": "Y1",
"lt": [
{
"Val34": "968"
}
]
},
{
"d23": "Y2",
"lt": [
{
"Val34": "958"
},
{
"Val34": "951"
}
]
}
]
`
Upvotes: 0
Views: 583
Reputation: 2431
You can try first groupingBy 2nd key value pair.. I assumed "d23": "Y1", "d23": "Y3" will be in position index[1]
.
Further you can map on grouped elements and access index[0]
%dw 2.0
output application/json
---
(payload groupBy ((item, index) -> item[1])) pluck $ map{
($[0][&1]),
"lt": ($ map(it,in)->(it[&0]))
}
Sample Input
[
{
"Val34": "968",
"d23": "Y1"
},
{
"Val34": "998",
"d23": "Y3"
},
{
"Val34": "988",
"d23": "Y1"
},
{
"Val34": "958",
"d23": "Y2"
},
{
"Val34": "951",
"d23": "Y2"
}
]
Output
[
{
"d23": "Y1",
"lt": [
{
"Val34": "968"
},
{
"Val34": "988"
}
]
},
{
"d23": "Y3",
"lt": [
{
"Val34": "998"
}
]
},
{
"d23": "Y2",
"lt": [
{
"Val34": "958"
},
{
"Val34": "951"
}
]
}
]
Upvotes: 1
Reputation: 759
Group by d23 values and map all the rest:
%dw 2.0
output application/json
---
payload groupBy ((item, index) -> item.d23) pluck {
d23: $[0].d23,
lt: ($.Val34 map (l,indOfl) -> {Val34: l})
}
Upvotes: 2