My top 5 DataWeave tips to make your life easier
It took me some time to find and fully understand how to use these “hacks” (they’re not really hacks, but they’ve made my life so much easier, so I’ll call them hacks). Trust me; they’re truly worth it, especially if you’re just starting to learn DataWeave.
1. Using the DataWeave Playground
This is my absolute favorite tool, and I really hope more functionality gets added into it (cough, cough, a DataWeave Notebook, please?). I use this tool ALL the time.
This is basically a tool that lets you experiment with DataWeave without doing it from Anypoint Studio. You can find the docker image to install in your local machine in this post (no previous Docker experience needed) - How to run locally the DataWeave Playground Docker Image.
But wait! Before you do that, the awesome DataWeave team already created an online site that you can access without having to run Docker. Check it out here: https://developer.mulesoft.com/learn/dataweave

2. Using the “dw” output
When you’re working with certain data types, like JSON, you don’t have the same visibility as some of the values’ types. For example, JSON converts the Dates into Strings. You can’t be absolutely sure about the exact type of output unless you follow the code. Here’s an example:
%dw 2.0
output application/json
---
"2000-01-01" as Date // "2000-01-01"
(I’m adding in the comments the output that I’m getting)
Simply by looking at “2000-01-01”, you’re not sure if it’s a String or a Date (without looking at the code, of course).
If I remove the “as Date” coercion, the output remains the same.
%dw 2.0
output application/json
---
"2000-01-01" // "2000-01-01"
Now try changing the “json” output for a “dw” output and see the magic.
Without coercion:
%dw 2.0
output application/dw
---
"2000-01-01" // "2000-01-01"
With coercion:
%dw 2.0
output application/dw
---
"2000-01-01" as Date // |2000-01-01|
Ah! You can see the difference now!
And that’s not all. By using this output, you can also see a function’s definition. Here’s an example of the “plusplus” function:
%dw 2.0
output application/dw
---
++ // (arg0:Array | String | Object | Date | LocalTime | Date | Time | Date | TimeZone | LocalDateTime | TimeZone | LocalTime | TimeZone, arg1:Array | String | Object | LocalTime | Date | Time | Date | TimeZone | Date | TimeZone | LocalDateTime | TimeZone | LocalTime) -> ???
Pretty cool, right?
3. Using the “log” function
Isn’t it frustrating when a small part of your script is not working, and you’re not sure why? You can’t actually debug it like you’d debug a Mule Flow. But guess what? You can output expressions into the console.
Let’s say that I have the following script:
%dw 2.0
output application/json
---
(random() * 100) > 74
I’m either getting true or false (Boolean, not String), depending on whether the randomly generated number is more than 74 or not, but I don’t know what the actual random number is.
Well, I can use the “log” function to see which number it is. And the best part? Adding this into my script doesn’t affect anything I do. I don’t have to add a new variable only for the log. I can just surround the value that I want to log into the console in parentheses.
%dw 2.0
output application/json
---
log(random() * 100) > 74
This is what it looks like from the DataWeave Playground: