0

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.

1
  • What exactly do you want to do when specialityIds has more than one element? By the expected output showed you want to return only the last one. You didn't explain exactly what you what's the outcome expected. Please ensure that the expected output is clear and there is an explanation of the criteria used. Commented Oct 30, 2022 at 3:24

4 Answers 4

0

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: $}
Sign up to request clarification or add additional context in comments.

6 Comments

hi @aled the expected output is not to retrieve the last element, but all elements and output in the structure. I've edited the example to make it clearer
Updated to new expected output. Can't test right now but should work or nearly.
thanks, this worked. there was an extra 'i' in your post and added the distinct constraint flatten(payload.data.*specialtyIds) map {id: $} distinctBy $
cause by writing code in a phone. For future questions please clarify exactly what is the expected outcome. The need to have distinct IDs was not explicit nor can be deduced from the output.
I'm sorry if you feel that but I don't believe we should guess parts of the problem from a single output. I can make sometimes educated guesses but not always. It is just not fair to people trying to answer in good faith. I missed the distinctBy however I had no way to know how significant it was.
|
0

It looks quite simple, not sure if I am answer correctly, here the solution

payload.data.map(item => ({id: item.specialtyIds.join(", ")}))

and you can do the following for ", " without space after (ps: you can't have "," as join, so join() will give you "," by default)

payload.data.map(item => ({id: item.specialtyIds.join()}))

Run from Console

3 Comments

this returns null for each key
are you using js or? I just tried and it works
This is a DataWeave language question. Though if you remove the . in the map and join with a joinBy and => to -> you mostly got it
0

Try this, %dw 2.0 output application/json

flatten(payload.data.*specialtyIds) distinctBy $ map ({ id: $ })

Comments

0

I think the solution will meet all your requirements:

%dw 2.0
output application/json
---
flatten(flatten(payload.data.specialtyIds) map ( $ splitBy ",")) distinctBy $ map ({ id: $ })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.