Four MuleSoft DataWeave Idiosyncrasies

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 {982924e26b728d6bee9b2e98d8d77350a8657cb0c945e2162fc618bd8dfd1bea}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:

{982924e26b728d6bee9b2e98d8d77350a8657cb0c945e2162fc618bd8dfd1bea}function test()
using (value1 = “abc”)
(
using (value2 = value1 ++ “d”)
value2
)

but this second expression fails:

{982924e26b728d6bee9b2e98d8d77350a8657cb0c945e2162fc618bd8dfd1bea}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 the author

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 BIG

Bits In Glass is an award-winning software consulting firm. We combine our deep industry expertise and experience with the best names in technology to provide innovative solutions to your unique business challenges. Our expert consultants excel at solving complex technical problems across industries and verticals, specializing in healthcare, financial services, insurance, and the public sector. Let us help you improve operations, drive better customer experiences, and return more to your bottom line.

Subscribe to Bits In Glass

Get the latest news and updates directly in your inbox.

Check out our video library!

Learn how Appian helped top brands gain success.