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
Application XML File (reformatted for readability):
<?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:
%dw 2.0
output application/json
---
payload pluck (value,key) -> {
Test: {
Key: key,
Value: value
}
}
When the the Mule app executes with the following JSON payload:
{
"firstName": "jason", "job": "engineer", "dept":"support"
}
The DataWeave transformation result is:
[
{
"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"