How to use VM arguments in Dataweave

How to use VM arguments in Dataweave

Are you wondering how to use VM arguments of your Mule application into Dataweave expressions and scripts? In this article, I’ll show you how simple the process is.

Dataweave allows you to transform, enrich and adapt your payloads, variables, and contents into your Mule applications. As soon as you’re willing to include some parametrization, here comes a helpful tool: the VM arguments.

Mule lets you enrich the VM arguments with additional and custom arguments according to your needing, but your questioning: “How could I use the into my Dataweave expressions and scripts?”.

Before answering this question, I’ll introduce myself.

My name is Lorenzo Neri, a Cap4 Lab integration engineer. Several times during my working time, I met many different problems I tried to solve, then afterward, a question came to my mind: “What if I bring help to other people to solve my same problem?”.

VM arguments in Dataweave expression: how to include them

If you’re reading this article, there’s a slight chance that you struggled several times with the “dollar operator”.

Dataweave and VM arguments have in common the special “$” operator, which has different usage.

You might have experienced some issues due to this “misinterpretation”: not you in this case but the Mule’s script reading.

The first ingredient to use VM arguments in Dataweave expression and scripts is indeed the dollar sign like you already do into connector’s parametrization: let’s explain with an example.

 

Let’s assume that you’ve got this custom VM argument:

-M-Dmule.redirectURL=aFakeURLAddress

Nowadays, to use it as a connector, you refer to it like:

${mule.redirectURL}

With Dataweave, things could be “more easier”: how? Let’s do another example.

Referring to the same VM argument, let’s say that you’re after giving that value to a JSON object, like:

{
  "redirectAddress": ${mule.redirectURL}
}

This is the struggle I was talking about a couple of paragraphs behind us: this will throw errors because Dataweave will misinterpret the dollar operator.

 

What you’ll need to solve this problem to use your VM argument into Dataweave properly is to pass it as a pure string.

If you quote your VM Arguments, it’ll work as desired:

{
  "redirectAddress": "${mule.redirectURL}"
}