How to use the “Using” Operator in DataWeave, the MuleSoft Mapping Tool
By Brian Labelle
I recently had a business problem which required using the “using” operator in MuleSoft’s DataWeave. I noticed there wasn’t a lot of documentation on it, so this will be a brief overview of my problem and how the using operator helped me.
The using operator allows us to use a variable in scope, for example, within a map. This is useful when you need to aggregate a subset of elements in an array. Let’s say we have an array with the following elements:
Our requirement is to display the letter and count for A, B and C.
We can write the following Data Weave:
which will give the result
Now the user says, I love your program but… I would like to have the sum of E and D counts. A simple solution is to use the using operator. You can create a function to sum the count of D and E and put that into a variable. You can use that variable to display the count within the map.
which will now give the result:
As you can see the using operator is a powerful feature but remember, as Stan Lee says, “with great power comes great responsibility”.
About the author
Brian is a MuleSoft Practice Lead at Bits In Glass. He’s as excited about toying around with DataWeave as he was pushing and popping stacks of bits into registers in the early days of coding. He has seven siblings and four children so working as a team is second nature to him.