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

  • Beyond Clicking Buttons: Build a Browser Agent That Verifies Its Results With Playwright MCP
  • Predict, Repeat, Improve: Deterministic Simulation Testing Explained
  • Jakarta Faces Flow Scope: Managing Multi-Step UX Without Session State
  • The Silent Container Death: A TCP Dial That Never Times Out
  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
7.2K 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. 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

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