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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Coordinating Threads Using CountDownLatch
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • MuleSoft APIkit Router - QueryParam and Header Strict Validation Configuration
  • Multi-Threaded Geo Web Crawler In Java

Trending

  • Automating Data Pipelines: Generating PySpark and SQL Jobs With LLMs in Cloudera
  • Measuring the Impact of AI on Software Engineering Productivity
  • How to Convert XLS to XLSX in Java
  • Mastering Advanced Traffic Management in Multi-Cloud Kubernetes: Scaling With Multiple Istio Ingress Gateways

Mule 4 - How to Pass Dynamic Values for "keys" in Key-Value Pair

Learn how to pass dynamic values for "keys" in key-value pair in Mulesoft DataWeave 2.0.

By 
Sravan Lingam user avatar
Sravan Lingam
DZone Core CORE ·
Feb. 08, 21 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
29.3K Views

Join the DZone community and get the full member experience.

Join For Free

Hello Muleys,

You might have quite often encountered a situation where you need to have dynamic "keys" in a key-value pair.

We know that we can pass dynamic values directly using the syntax like below  : 

Java
 




x


 
1
%dw 2.0
2
output application/java
3
---
4
{
5
"message" : vars.message
6
}



But even the key "message" has to be dynamic. Then what's the syntax we have to use?

Simple! - 

Define a local variable,

"$(a)"  - Just wrap  within double quotes and give your local var within ()

Double quotes are very important. Or else you will have a syntactical error.

Also its () not {} . There's a difference between $() and ${}

${} is used to retrieve property value. Whereas $() is used to retrieve dynamic value.

Eg:

Java
 




x


 
1
%dw 2.0
2
output application/json
3
var a = "message"
4
---
5
{
6
"$(a)" : payload
7
}


The above syntax returns:

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "message": "Hello Muleys"
3
}



If you want dynamic keys for an array of objects,

then you can use like ($.xyz) you need to wrap in ().

Java
 




xxxxxxxxxx
1
15


 
1
%dw 2.0
2
output application/json
3
 
          
4
var a = "OrderInformation"
5
var orders = [
6
    {orderId : 134 , orderName :  "Soap"},
7
    {orderId : 222 , orderName :  "Tumbler"},
8
    {orderId : 389 , orderName :  "NoteBook"}
9
]
10
--- 
11
"$(a)" : orders map {
12
    ($.orderId) : $.orderName
13
                    }
14
     
15
 
          


Result :

JSON
 




xxxxxxxxxx
1
13


 
1
{
2
  "OrderInformation": [
3
    {
4
      "134": "Soap"
5
    },
6
    {
7
      "222": "Tumbler"
8
    },
9
    {
10
      "389": "NoteBook"
11
    }
12
  ]
13
}



So basically 2 different syntaxes for dynamic keys. 

One:  When you want to use the local variable as the key-value - "$(a)"

Two: When you want to use the array of values as keys  -  ($.xyz)


Here's the video for the same :


Simple and Short!
Happy Learning!


Pass (software)

Opinions expressed by DZone contributors are their own.

Related

  • Coordinating Threads Using CountDownLatch
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically
  • MuleSoft APIkit Router - QueryParam and Header Strict Validation Configuration
  • Multi-Threaded Geo Web Crawler In Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!