I have a payload like below:
{
"data": [
{
"id": "f251f05f-038c-4c26-bf7c-3b2fc47210e6",
"specialtyIds": [
"20c5f3f0-54c9-4779-b1a3-19baeee91b4a"
]
},
{
"id": "61d34a84-940d-4556-9c4b-ef7bede9caca",
"specialtyIds": [
"20c5f3f0-54c9-4779-b1a3-19baeee91b4a",
"9834e1cf-94c4-4188-83e6-867ac1d60017",
"30d6g4d3-54c9-4779-b1a3-19baeee92cdc"
]
}
]
}
and want to return an array like:
[
{
id: "20c5f3f0-54c9-4779-b1a3-19baeee91b4a"
},
{
id: "9834e1cf-94c4-4188-83e6-867ac1d60017"
},
{
id: "30d6g4d3-54c9-4779-b1a3-19baeee92cdc"
}
]
I've used the following dataweave which works fine when specialtyIds
is only one element. But the second there's more than one element it breaks:
payload.data map {
id: $.specialtyIds joinBy(",")
} distinctBy $
if the array has more than two elements the script returns:
[
{
id: "20c5f3f0-54c9-4779-b1a3-19baeee91b4a"
},
{
id: "20c5f3f0-54c9-4779-b1a3-19baeee91b4a,9834e1cf-94c4-4188-83e6-867ac1d60017"
}
]
I am relatively new to dataweave, but have explored pluck and reduce to iterate over the arrays but haven't had much luck. I feel like there is probably a simpler way to tackle this structure.
This script gets the last element of each specialityIds arrays and returns the exact output that the question shows as the expected output.
%dw 2.0
output application/json
---
flatten(payload.data.*specialtyIds) map {id: $}