Four MuleSoft DataWeave Idiosyncrasies

Blog, Mulesoft

Four MuleSoft DataWeave Idiosyncrasies

By Adam Lounsbury

As MuleSoft developers know, DataWeave is a powerful expression language for manipulating and transforming a variety of data types. However, just like any software, there are times where it doesn’t necessarily perform as expected. Below is a list of unusual behaviors I’ve noticed with DataWeave 1.0 over the past couple of years.

1. Trying to cast a double to an integer when the output type is application/json.

When the DataWeave output mimeType is application/java for the following expression:

256.0666 as :number {class: “java.lang.Integer”}

the expression functions as expected, with the DataWeave preview correctly displaying 256.

However, if the output mimeType is set to application/json, the output will show 256.0666. The casting does not work, despite the output being valid JSON. It’s possible that under-the-hood, DataWeave is unable to invoke the underlying Java casting due to a mimeType conflict. The current workaround for this is to use the expression 256.0666 as :string {format: “#”} as :number.

2. Defining a variable with the string ‘true’ or ‘false’ prefixing the variable name.

An error is thrown if you define variable %var trueLocationId = ‘foo’ and attempt to reference it in any way.

The error reported is: Invalid input “LocationId,”, expected is, *, <=, <, operatorName, ‘#’, >=, as, :name, >>, ~=, alphaNumUnderscore, -, selectors, functionCall, ==, +, !=, objectEnd, arraySep, :, / or >

It appears that the parser is reading the first 4 characters of the variable as a reserved word, then throwing an error based on that. Note that the error refers to the input as LocationId, not trueLocationId. If you place the string ‘true’ anywhere else in the variable name, no error is thrown, so order matters and it must be prefixed to the variable name for this error to occur.

3. Removing an element from an arrayList that the pluck function generates.

pluck is a useful DataWeave function for extracting keys from an object, generating an arrayList containing said keys.

However, if one attempts to use pluck on a payload, e.g. payload.bar pluck $$, and subsequently attempts to use the operator to remove an element from the resultant arrayList, the element removal fails. After some investigation, by viewing the contents of the arrayList as output type application/java, I learned that pluck by default returns QualifiedName objects inside the arrayList (as opposed to strings/primitives). Thus, using the operator will not remove the item.

In order to have element removal work as intended, you must pluck, then coerce the resultant objects to the primitive of your choice prior to attempting removal e.g.) payload.bar pluck $$ as :string.

4. Local scoping with the using operator.

The using operator allows for local variable usage strictly within a scope. However, the scope is strictly processed.

For example, the following expression is valid:

%function test()
             using (value1 = “abc”)
                    (
                               using (value2 = value1 ++ “d”)
                                   value2
                    )

but this second expression fails:

          %function test2()
            using (value1 =“abc”, value2 = value1 ++ “d”)
                    value2

DataWeave under-the-hood appears to parse the entire using expression as a whole, rather than sequentially left-to-right (or even right-to-left). As such, using operators must be nested, as shown in the first example above.

DataWeave is one of the best tools a MuleSoft developer has at their disposal to create world-class integration solutions. Despite the occasional counter-intuitive behavior, leveraging DataWeave allows us to create robust, easy-to-maintain data mappings that can grow and evolve with a customer’s needs.

Like this post? Find out more about using MuleSoft’s Dataweave operators here.

About Adam

Adam C Lounsbury

Adam is a MuleSoft Developer at Bits In Glass. A technology enthusiast from a young age, Adam is right at home working with software. He enjoys living an active lifestyle, camping, and traveling.

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).

For more information about Bits In Glass, visit www.bitsinglass.com and follow us on LinkedInTwitter, or Instagram.