Deep Dive Into DataWeave Pluck Operator
See a deep dive into DataWeave pluck operator with examples and videos.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
The pluck operator is useful for mapping the object into the array, and it returns the array of values, keys, and indexes of the object.
$ will return values, $$ will return keys and $$$ will return indexes.
x
%dw 2.0
output application/json
var requestBody=
{
"name":"Jack",
"name":"James",
"name":"Joseph"
}
---
{
value: requestBody pluck $,
keys: requestBody pluck $$,
indexes: requestBody pluck $$$
}
Output
xxxxxxxxxx
{
"value": [
"Jack",
"James",
"Joseph"
],
"keys": [
"name",
"name",
"name"
],
"indexes": [
0,
1,
2
]
}
Example 1
We have a list of car objects in a single JSON, but we need to convert that car object into an array of cars or a list of cars in a single array. We can use the pluck operator to achieve this as shown below.
xxxxxxxxxx
%dw 2.0
output application/json
var requestBody=
{
"car" :
{
"Make&Model": "Maruti Suzuki Swift",
"MakeYear": 2017,
"Color": "Red",
"Type": "Hatchback",
},
"car" :
{
"Make&Model": "Maruti WagonR",
"MakeYear": 2018,
"Color": "Brown",
"Type": "Hatchback",
},
"car" :
{
"Make&Model": "Honda City",
"MakeYear": 2017,
"Color": "Red",
"Type": "Sedan",
},
"car" :
{
"Make&Model": "Ford Figo",
"MakeYear": 2017,
"Color": "Black",
"Type": "Hatchback",
}
}
---
car:requestBody pluck $
Output
xxxxxxxxxx
{
"car": [
{
"Make&Model": "Maruti Suzuki Swift",
"MakeYear": 2017,
"Color": "Red",
"Type": "Hatchback"
},
{
"Make&Model": "Maruti WagonR",
"MakeYear": 2018,
"Color": "Brown",
"Type": "Hatchback"
},
{
"Make&Model": "Honda City",
"MakeYear": 2017,
"Color": "Red",
"Type": "Sedan"
},
{
"Make&Model": "Ford Figo",
"MakeYear": 2017,
"Color": "Black",
"Type": "Hatchback"
}
]
}
Example 2
We need to convert the car object into an array of cars and filter those cars having the color red. In this case, we can use the pluck operator with a filter to achieve this as shown below.
xxxxxxxxxx
%dw 2.0
output application/json
var requestBody=
{
"car" :
{
"Make&Model": "Maruti Suzuki Swift",
"MakeYear": 2017,
"Color": "Red",
"Type": "Hatchback",
},
"car" :
{
"Make&Model": "Maruti WagonR",
"MakeYear": 2018,
"Color": "Brown",
"Type": "Hatchback",
},
"car" :
{
"Make&Model": "Honda City",
"MakeYear": 2017,
"Color": "Red",
"Type": "Sedan",
},
"car" :
{
"Make&Model": "Ford Figo",
"MakeYear": 2017,
"Color": "Black",
"Type": "Hatchback",
}
}
---
car:requestBody pluck $ filter $.Color == "Red"
Output
xxxxxxxxxx
{
"car": [
{
"Make&Model": "Maruti Suzuki Swift",
"MakeYear": 2017,
"Color": "Red",
"Type": "Hatchback"
},
{
"Make&Model": "Honda City",
"MakeYear": 2017,
"Color": "Red",
"Type": "Sedan"
}
]
}
Example 3
We are getting address as a Object in the JSON message but destination is expecting address in single field. In this case, we can use the pluck operator with joinBy to achieve this as shown below.
joinBy merges an array into a single string value and uses the provided string as a separator between each item in the list.
%dw 2.0
output application/json
var requestBody=
{
"name":"James Peter",
"age":35,
"salary":30000.00,
"addressDetails":
{
"street": "101 Oxford Street",
"city":"London",
"country":"United Kingdom",
"postalCode": "W1U TR4"
}
}
---
{
name: requestBody.name,
age: requestBody.age,
salary: requestBody.salary,
address: requestBody.addressDetails pluck $ joinBy ","
}
Output
xxxxxxxxxx
{
"name": "James Peter",
"age": 35,
"salary": 30000.00,
"address": "101 Oxford Street,London,United Kingdom,W1U TR4"
}
DataWeave Transformation (Pluck and Reduce) With MuleSoft
Reading Attachments in MuleSoft | MultiPart | Form-Data | Pluck
Now, you know how to use the pluck operator with MuleSoft DataWeave.
Opinions expressed by DZone contributors are their own.
Comments