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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why AI Testing Needs Confidence Scores, Not Just Pass/Fail Results
  • When Data Quality Checks Pass but the Data Is Still Stale
  • Coordinating Threads Using CountDownLatch
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Trending

  • Prompt Caching for LLMs, Part 1: Exact-Match Caching in Production
  • Engineering Data Observability: Detecting Schema Drift, Data Drift, and Silent Pipeline Failures
  • Three Measured Outcomes of Manually Scaling Fleet in Prod
  • One Identity Unveils Active Roles 8.6 with Enhanced Security and Administration

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

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
·
Feb. 08, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
30.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • Why AI Testing Needs Confidence Scores, Not Just Pass/Fail Results
  • When Data Quality Checks Pass but the Data Is Still Stale
  • Coordinating Threads Using CountDownLatch
  • Mule 3 DataWeave(1.x) Script To Resolve Wildcard Dynamically

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook