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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Trending

  • Build Software Faster With Three Simple Principles
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Kafka With Spring Boot: Introduction
  • When One Docker Swarm Replica Became Unreachable Over an Overlay Network: A Production Case With Traefik

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Dec. 18, 20 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Testing Business Programs Without Constructing Domain Objects
  • Momento Migrates Object Cache as a Service to Ampere® Altra®
  • Why I Ditched Redis for Cloudflare Durable Objects in My Rate Limiter
  • Real-Object Detection at the Edge: AWS IoT Greengrass and YOLOv5

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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