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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management
  • A Comprehensive Guide to IAM in Object Storage

Trending

  • Start Coding With Google Cloud Workstations
  • How to Build Scalable Mobile Apps With React Native: A Step-by-Step Guide
  • Scaling Mobile App Performance: How We Cut Screen Load Time From 8s to 2s
  • Chaos Engineering for Microservices
  1. DZone
  2. Coding
  3. Languages
  4. Understanding DataWeave's Pluck Function

Understanding DataWeave's Pluck Function

The pluck function is used to retrieve the key, value, and index from an object and output the result to an array.

By 
Alex Theedom user avatar
Alex Theedom
·
Dec. 18, 20 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

Main Points

  • the pluck function iterates over each key/value pair of the input object.
  • it retrieves the keys, values, and indexes from the input object.
  • input to the pluck function is an object, and output is an array.

What Is the Pluck Function Used For?

The pluck function is used to retrieve the key, value, and index from an object and output the result to an array. The code snippets in figures 1, 2, and 3 show the retrieval of the object’s key, value, and index using the full reference and the built-in default.

{pet: "cat", name: "Ponsonby"} pluck (value, key, index) -> key
or
{pet: "cat", name: "Ponsonby"} pluck $$
OUTPUT: ["pet", "name"]


Figure 1: Use the pluck function to retrieve all keys from the input object.


{pet: "cat", name: "Ponsonby"} pluck (value, key, index) -> value
or
{pet: "cat", name: "Ponsonby"} pluck $
OUTPUT: ["cat", "Ponsonby"]


Figure 2: Use the pluck function to retrieve the values of all the values in the input object.


{pet: "cat", name : "Ponsonby"} pluck (value, key, index) -> index
or
{pet: "cat", name: "Ponsonby"} pluck $$$
OUTPUT: [0, 1]


Figure 3: Use the pluck function to retrieve the index of all elements in the input object.


How Does the Pluck Function Work?

The pluck function iterates over each key and value pair of the input object and extracts its keys, values, and indexes, and compiles the result to an array. You have a choice and can use the full reference to key, value, and index or the built-in defaults $$, $ and $$$ (respectively).

Identify the key, value and index of the input array

Figure 4: Identify the key, value, and index of the input array.

Use the built-in defaults to refer key, value, and index.

Figure 5: Use the built-in defaults to refer key, value, and index.

An Example of a Combined Use of Pluck

Here are two further examples of the use of the pluck function. In figure 6, each key and value pair is extracted into an object, which compiles into an array.

payload pluck {
  key: $$,
  value: $,
  index: $$$
}
OUTPUT:
[
  {
    "key": "pet",
    "value": "cat",
    "index": 0
  },
  {
    "key": "name",
    "value": "Ponsonby",
    "index": 1
 }
]


Figure 6: Extract all parts of an object


In figure 7, all the keys, values, and indices are extracted into separate arrays:

{
  keys: payload pluck $$,
  values: payload pluck $,
  indices: payload pluck $$$
}
OUTPUT:
{
  "keys": [ "pet", "name" ],
  "values": [ "cat", "Ponsonby" ],
  "indices": [ 0, 1 ]
}


Figure 7: Extract all keys, values, and indices


Conclusion

Typically the pluck function would be used in collaboration with other functions such as the DataWeave map function or the mapObject function.

Object (computer science)

Published at DZone with permission of Alex Theedom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Cutting-Edge Object Detection for Autonomous Vehicles: Advanced Transformers and Multi-Sensor Fusion
  • Writing DTOs With Java8, Lombok, and Java14+
  • Graph API for Entra ID (Azure AD) Object Management
  • A Comprehensive Guide to IAM in Object Storage

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!