Loading

Dataweave- How do I extract key/value pairs from JSON payload and store them into flow variables with the keys and their corresponding values in Mule4.

Publish Date: Aug 5, 2025
Task

GOAL

This article shows you an example of how to extract key/value pairs from JSON payload and store them into flow variables with variable name as the keys and their corresponding values in Mule4.
Steps
Assuming that I have the following JSON payload, which contains three key/value pairs in JSON format:
{"firstName": "jason", "job": "engineer", "dept":"support"}
1. First, you would need to use the Pluck function in Dataweave, to extract out keys and values, and then put them into an array.
The pluck function is defined as:
payload pluck (value,key)  -> {
	Test:{
		Key: key,
		Value: value
	}
}
And the pluck function above will transform the original JSON payload to the following payload:
[
  {
    "Test": {
      "Key": "firstName",
      "Value": "jason"
    }
  },
  {
    "Test": {
      "Key": "job",
      "Value": "engineer"
    }
  },
  {
    "Test": {
      "Key": "dept",
      "Value": "support"
    }
  }
]
2. Then, you will need to iterate the above array payload. To perform this task, you will need to use the For Each Scope, with collection set as #[payload.Test] for the For Each Scope.
3. During each iteration, you will then set the flow variable name as the Key and variable value as the Value in the JSON array.
User-added image

Please refer to the sample project attached for more details.

 
Additional Resources
json-sample.jar
Knowledge Article Number

001114536

 
Loading
Salesforce Help | Article