How to Use the “Using” Operator in DataWeave, the MuleSoft Mapping Tool
By Brian Labelle, Bits In Glass
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 DataWeave:
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. And 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”.
If you’re interested in more, I’ve also got a post on the basics of Reduce, a MuleSoft DataWeave Operator.
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.
About Bits In Glass
Bits In Glass is an award-winning software consulting firm that helps companies outpace the competition, drive rapid growth, and deliver superior customer value through the use of technology.
Our expert consultants find the most innovative solutions to solve the most complex business challenges across multiple industries and verticals. With hundreds of years of in-house experience, we are the partner of choice for many business transformation projects, working with market leaders who are disrupting and driving transformation across every aspect of modern business.
Find out why leading technology companies partner with Bits In Glass including Appian (Business Process Management), MuleSoft (API-Led Systems Integration), Blue Prism (Robotic Process Automation) and Salesforce.com (Customer Relationship Management).