DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Trending

  • How Agentic AI Is Turning Traditional Automation Into a Tool Layer?
  • From APIs to Agents: How Back-End Engineering is Evolving in 2026
  • Building Production-Grade Semantic Search With GPT-5 and Microsoft Foundry, From Scratch
  • React 19 Killed Half My Performance Optimization Code, and I'm Grateful

Deep Dive Into DataWeave Pluck Operator

See a deep dive into DataWeave pluck operator with examples and videos.

By 
Jitendra Bafna user avatar
Jitendra Bafna
·
Jan. 13, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

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.

JSON
 




x


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
{
5
    "name":"Jack",
6
    "name":"James",
7
    "name":"Joseph"
8
}
9
---
10
{
11
value: requestBody pluck $,
12
keys: requestBody pluck $$,
13
indexes: requestBody pluck $$$
14
}



Output

JSON
 




xxxxxxxxxx
1
17


 
1
{
2
  "value": [
3
    "Jack",
4
    "James",
5
    "Joseph"
6
  ],
7
  "keys": [
8
    "name",
9
    "name",
10
    "name"
11
  ],
12
  "indexes": [
13
    0,
14
    1,
15
    2
16
  ]
17
}



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.

JSON
 




xxxxxxxxxx
1
37


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
{
5
"car" : 
6
{
7
"Make&Model": "Maruti Suzuki Swift",
8
"MakeYear": 2017,
9
"Color": "Red",
10
"Type": "Hatchback",
11
},
12
"car" : 
13
{
14
"Make&Model": "Maruti WagonR",
15
"MakeYear": 2018,
16
"Color": "Brown",
17
"Type": "Hatchback",
18
},
19
"car" : 
20
{
21
"Make&Model": "Honda City",
22
"MakeYear": 2017,
23
"Color": "Red",
24
"Type": "Sedan",
25
},
26
"car" : 
27
{
28
"Make&Model": "Ford Figo",
29
"MakeYear": 2017,
30
"Color": "Black",
31
"Type": "Hatchback",
32
}
33
}
34
---
35
car:requestBody pluck $



Output

JSON
 




xxxxxxxxxx
1
28


 
1
{
2
  "car": [
3
    {
4
      "Make&Model": "Maruti Suzuki Swift",
5
      "MakeYear": 2017,
6
      "Color": "Red",
7
      "Type": "Hatchback"
8
    },
9
    {
10
      "Make&Model": "Maruti WagonR",
11
      "MakeYear": 2018,
12
      "Color": "Brown",
13
      "Type": "Hatchback"
14
    },
15
    {
16
      "Make&Model": "Honda City",
17
      "MakeYear": 2017,
18
      "Color": "Red",
19
      "Type": "Sedan"
20
    },
21
    {
22
      "Make&Model": "Ford Figo",
23
      "MakeYear": 2017,
24
      "Color": "Black",
25
      "Type": "Hatchback"
26
    }
27
  ]
28
}



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.

JSON
 




xxxxxxxxxx
1
35


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
{
5
"car" : 
6
{
7
"Make&Model": "Maruti Suzuki Swift",
8
"MakeYear": 2017,
9
"Color": "Red",
10
"Type": "Hatchback",
11
},
12
"car" : 
13
{
14
"Make&Model": "Maruti WagonR",
15
"MakeYear": 2018,
16
"Color": "Brown",
17
"Type": "Hatchback",
18
},
19
"car" : 
20
{
21
"Make&Model": "Honda City",
22
"MakeYear": 2017,
23
"Color": "Red",
24
"Type": "Sedan",
25
},
26
"car" : 
27
{
28
"Make&Model": "Ford Figo",
29
"MakeYear": 2017,
30
"Color": "Black",
31
"Type": "Hatchback",
32
}
33
}
34
---
35
car:requestBody pluck $ filter $.Color == "Red"



Output

JSON
 




xxxxxxxxxx
1
16


 
1
{
2
  "car": [
3
    {
4
      "Make&Model": "Maruti Suzuki Swift",
5
      "MakeYear": 2017,
6
      "Color": "Red",
7
      "Type": "Hatchback"
8
    },
9
    {
10
      "Make&Model": "Honda City",
11
      "MakeYear": 2017,
12
      "Color": "Red",
13
      "Type": "Sedan"
14
    }
15
  ]
16
}



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. 

JSON
 




x
24


 
1
%dw 2.0
2
output application/json
3
var requestBody= 
4
{
5
    "name":"James Peter",
6
    "age":35,
7
    "salary":30000.00,
8
    "addressDetails":
9
    {
10
        "street": "101 Oxford Street",
11
        "city":"London",
12
        "country":"United Kingdom",
13
        "postalCode": "W1U TR4"
14
    }
15
}
16
---
17
{
18
    name: requestBody.name,
19
    age: requestBody.age,
20
    salary: requestBody.salary,
21
    address: requestBody.addressDetails pluck $ joinBy ","
22
}



Output

JSON
 




xxxxxxxxxx
1


 
1
{
2
  "name": "James Peter",
3
  "age": 35,
4
  "salary": 30000.00,
5
  "address": "101 Oxford Street,London,United Kingdom,W1U TR4"
6
}


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.

Operator (extension)

Opinions expressed by DZone contributors are their own.

Related

  • GitOps Secrets Management: The Vault + External Secrets Operator Pattern (With Auto-Rotation)
  • GitOps-Backed Agentic Operator for Kubernetes: Safe Auto-Remediation With LLMs and Policy Guardrails
  • Agentic AI: The Next Evolution of Artificial Intelligence and Autonomous Automation
  • Exploring C++23: Multidimensional Subscript Operator

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook