Dataweave Interview Question Using Map and Reduce
This article will help you practice your Dataweave skills in Mulesoft. Here we have to convert the input to a specific type of output.
This article will help you practice your Dataweave skills in Mulesoft. Here we have to convert the input to a specific type of output. Let's get started.
Input:
[
{
"param_name": "first",
"param_value": "second"
},
{
"param_name": "third",
"param_value": "fourth"
}
]
Output:
xxxxxxxxxx
{
"first": "second",
"third": "fourth"
}
Let's talk about the solution now. We are going to achieve this in two steps as follows.
Step 1
The below code is our desired output:
xxxxxxxxxx
[
{
"first": "second"
},
{
"third": "fourth"
}
]
In order to get output like this, we will use the below code:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload map ((item, index) -> (item.param_name):item.param_value)
Step 2
This will give us the final output. We use the below code to achieve the same:
xxxxxxxxxx
%dw 2.0
output application/json
---
payload map ((item, index) -> (item.param_name):item.param_value) reduce ((item, accumulator) -> accumulator ++ item)
I hope this helps improve your Dataweave skills. Thanks!
Opinions expressed by DZone contributors are their own.