maomifadacai
maomifadacai

Reputation: 369

how to get dynamic column in dataweave

I blow array

[ {"FF15974433790786634518": 1 },
{"FF15974433790786634518": 2 },
{"FF15974433790786634519": 1 }]

how to get ["FF15974433790786634518","FF15974433790786634519"] in dataweave?

Upvotes: 1

Views: 235

Answers (1)

Michael Jones
Michael Jones

Reputation: 1910

We can map each item and pluck the keys. This gives us an array of arrays, so instead we use flatMap (which is equivalent to flatten(payload map(.... After getting our flat array of keys, we can distinctBy to filter out the duplicates.

Edit: As commented by @machaval below, you can make this even more succinct and easier to read by using namesOf, which is a wrapper for pluck.

%dw 2.0
output application/json
---
payload flatMap namesOf($) distinctBy($)

What all of this is doing when expanded:

%dw 2.0
output application/json
---
flatten(
    payload map (item, index) -> (
        item pluck ((value, key, index) -> key)
    )
) distinctBy ((item, index) -> item)

Upvotes: 3

Related Questions