---
schema_version: "1"
id: 2b4886e9baf589c52221eeee493781c5217f0e5b62ba9d651800976b223ef963
title: Extract Key-Value Pairs with Pluck Function
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.9/dataweave-cookbook-pluck.md
canonical_url: https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook-pluck
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 6943d6d8a18ee96fedc4fc926bc4a9e01bd5964c3e8da5db2ec19a34dcbb55a1
description: MuleSoft Documentation Site
release: "2.9"
latest: false
---
---
product: DataWeave
version: 2.9
keywords: studio, anypoint, transform, transformer, format, dataweave, data weave, output structure, mime type, mime
---

# Extract Key-Value Pairs with `Pluck` Function

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

In addition to using the DataWeave functions such as [entriesOf](https://docs.mulesoft.com/dataweave/2.9/dw-core-functions-entriesof),
[keysOf](https://docs.mulesoft.com/dataweave/2.9/dw-core-functions-keysof), or [valuesOf](https://docs.mulesoft.com/dataweave/2.9/dw-core-functions-valuesof) to work with key-value
pairs, you can also use [pluck](https://docs.mulesoft.com/dataweave/2.9/dw-core-functions-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](/mulesoft/current/en-us/dataweave/2.9/_images/dataweave-cookbook-pluck-examples.png)

Application XML File (reformatted for readability):

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
      xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
      <flow name="json-varsFlow" >
            <scheduler doc:name="Scheduler" >
	          <scheduling-strategy >
		        <fixed-frequency frequency="10000"/>
                  </scheduling-strategy>
	    </scheduler>
	    <set-payload value='{"firstName": "jason", "job": "engineer", "dept":"support"}'
	                 doc:name="Set Payload"
			 mimeType="application/json" />
	    <ee:transform doc:name="Transform Message" >
	          <ee:message >
		        <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---

payload pluck (value,key)  -> {
	Test:{
		Key: key,
		Value: value
	}
}]]></ee:set-payload>
                  </ee:message>
            </ee:transform>
	    <foreach doc:name="For Each"
	             collection="#[payload.Test]">
	                <logger level="INFO" doc:name="Logger" message="#[payload]" />
		        <set-variable value="#[payload.Value]"
		                      doc:name="Set Variable"
				      variableName="#[payload.Key]"/>
		        <logger level="INFO"
		                doc:name="Logger"
			        message="#[vars[payload.Key]]"/>
            </foreach>
      </flow>
</mule>
```

Note that the Transform Message configures the following DataWeave script:

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

When the Mule app executes with the following JSON payload:

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

The DataWeave transformation result is:

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

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

INFO 2022-06-22 10:11:17,864 \[\[MuleRuntime\].uber.02: \[jsonsample\].json-varsFlow.CPU\_INTENSIVE
@5c652aa\] \[processor: json-varsFlow/processors/2/processors/0; event:
545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: { "Key": "firstName", "Value":
"jason" } INFO 2022-06-22 10:11:17,870 \[\[MuleRuntime\].uber.02:
\[jsonsample\].json-varsFlow.CPU\_INTENSIVE @5c652aa\] \[processor:
json-varsFlow/processors/2/processors/2; event: 545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "jason" INFO 2022-06-22
10:11:17,871 \[\[MuleRuntime\].uber.02: \[jsonsample\].json-varsFlow.CPU\_INTENSIVE @5c652aa\]
\[processor: json-varsFlow/processors/2/processors/0; event: 545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: { "Key": "job", "Value": "engineer"
} INFO 2022-06-22 10:11:17,875 \[\[MuleRuntime\].uber.02:
\[jsonsample\].json-varsFlow.CPU\_INTENSIVE @5c652aa\] \[processor:
json-varsFlow/processors/2/processors/2; event: 545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "engineer" INFO 2022-06-22
10:11:17,877 \[\[MuleRuntime\].uber.02: \[jsonsample\].json-varsFlow.CPU\_INTENSIVE @5c652aa\]
\[processor: json-varsFlow/processors/2/processors/0; event: 545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: { "Key": "dept", "Value": "support"
} INFO 2022-06-22 10:11:17,879 \[\[MuleRuntime\].uber.02:
\[jsonsample\].json-varsFlow.CPU\_INTENSIVE @5c652aa\] \[processor:
json-varsFlow/processors/2/processors/2; event: 545f5ef0-f24e-11ec-8c92-147ddaaf4f97\]
org.mule.runtime.core.internal.processor.LoggerMessageProcessor: "support"

## See Also

- [DataWeave Formats](https://docs.mulesoft.com/dataweave/2.9/dataweave-formats)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.9/dataweave-cookbook)
