0
votes

In Mulesoft Dataweave I want to map an object to an array.

Input:

[
  {
     "id": 12, "name": "sun", "age": 25
  },
  {
     "id": 13, "name": "moon", "age": 25
  },
  {
     "id": 14, "name": "pluto", "age": 28
  },
  ..... etc.
]

And the result must be this:
[12,13,14]

I have tried it with a map but it does not wor because it seems I am tied to using json objects.

How can I solve this ?

1
I would suggest you edit your question with valid JSON - the keys have to be wrapped in quotes. - Michael Jones

1 Answers

2
votes

We don't even need to map:

input:

[
  {
     "id": 12, "name": "sun", "age": 25
  },
  {
     "id": 13, "name": "moon", "age": 25
  },
  {
     "id": 14, "name": "pluto", "age": 28
  }
]

dataweave:

%dw 2.0
output application/json
---
payload.id

if you wanted to map though you could simply do:

payload map $.id

payload map (item) -> item.id

payload map (item) -> (item.id)

map(payload, (item) -> item.id))

all of which would give you the same result

If you were trying: payload map { ..., you'd get an object back out because of the {.

output:

[
  12,
  13,
  14
]