November 21 2018


Often when learning a new tool, IDE or language depending on how curious you are or how much time you have to get to play with it you get to discover many different things about it.

This time around when learning Mulesoft a few months ago using both 3/4 Anypoint Studios I had to learn of course the basic components and once I felt comfortable with them I started using the not so common components (based on the tutorials I did).

I'll talk about the experience that I had using the scripting component using the JavaScript engine and since there's no much out there (Most examples in the documentation are about Python and Groovy) I'll share with you the few key things I found useful.

The following techniques apply to both Mule 3 or 4 unless I say otherwise.

Read the payload using the eval() JS function

var payloadVar = eval('(' + payload + ')');

Reading the payload like this will allow you to treat it as a JavaScript object therefore use native JavaScript functions to work with objects, iterate arrays or filter them.

For instance ->

var payloadVar = eval('(' + payload + ')');

var items = payloadVar.listOfItems;

var serviceItems = items.filter(function(i) {return i.itemType == 'Service'});

Log information through the script in the Mule console

Pretty simple, yet not documented. You can use the function to give detailed information in the console when the script is being executed.

Like this ->

var simpleString = "simple";"Hello World");;

Returning the payload as JSON

It's a bit more critical in Mule 3 because if you don't return it as JSON and then you pass the payload for instance to the response of a call. It will be read as 'unexpected c'.

In Mule 4 it's pretty much the same. The difference is that if you return the payload as it is it will be treated in the flow as a HashMap generated by the Nashorn JavaScript engine.

var payloadToReturn = {
	customerName: payloadVar.customerName,
	customerId: payloadVar.customerId,
	orderNumber: payloadVar.orderNumber,
	totalServicesCost: 23453,
	totalItemCost: 345,
	totalCost: 2798

//mule 3
payload = JSON.stringify(payloadToReturn);

//mule 4
payload = payloadToReturn;


How to deal with Flow Variables

Mule 3

Let's say there's a set variable outside the script that sets variable1 = "I am variable 1"

var simpleString = flowVars.get('variable1');;
//output: "I am variable 1"

flowVars.put('variable1', "I'm variable 1 changed");'variable1'));
//output: "I'm variable 1 changed"

Mule 4

Same scenario with the set variable component

var simpleString = vars.variable1;;
//output: "I am variable 1"

vars.variable1 = "changed";
//this doesn't work'variable1'));
//output: "I'm variable 1 changed"

About the Author


My name is Alvaro Zorzini I'm from Buenos Aires, Argentina. I'm a student of computer science engineering and been working for AVIO since 2014 as a Full Stack Developer. 

Join the Conversation

Enter your first name. It will only be used to display with your comment.
Enter your email. This will be used to validate you as a real user but will NOT be displayed with the comment.