Hear from Salesforce leaders on how to create and deploy Agentforce agents.
Contact Us 1-800-596-4880

Extract Key-Value Pairs with Pluck Function

In addition to using the DataWeave functions such as entriesOf, keysOf, or valuesOf to work with key-value pairs, you can also use pluck. The following Mule app example shows how to use the DataWeave pluck function to extract key-value pairs from a JSON payload and store them into Mule event variables with variable names as the keys and their corresponding values.

The Mule app consists of:

  • A Scheduler component that triggers the flow

  • A Set Payload component that sets the JSON payload

  • A Transform Message component that transforms the content read from the Set Payload component by extracting the key-value pairs with the pluck function and putting them into an array

  • A For Each scope component that iterates over the array

  • A Set Variable component that sets the variable name as the Key and variable value as the Value in the JSON array

  • A Logger component that prints the variable results per each iteration

Mule app flow example in Studio canvas

Application XML File (reformatted for readability):

Note that the Transform Message configures the following DataWeave script:

%dw 2.0
output application/json
---
payload pluck (value,key)  -> {
  Test: {
    Key: key,
    Value: value
  }
}
dataweave

When the Mule app executes with the following JSON payload:

{
  "firstName": "jason", "job": "engineer", "dept":"support"
}
json

The DataWeave transformation result is:

[
  {
    "Test": {
      "Key": "firstName",
      "Value": "jason"
    }
  },
  {
    "Test": {
      "Key": "job",
      "Value": "engineer"
    }
  },
  {
    "Test": {
      "Key": "dept",
      "Value": "support"
    }
  }
]
json

The For Each scope component iterates over the transformed values, and the app logs the values of the variable results (reformatted for readability):