Many MuleSoft customers learn about DataWeave during one of our MuleSoft training sessions. But if you’re still unfamiliar with it, DataWeave is a powerful transformation language that’s mainly used inside a Mule application. But did you know that DataWeave can also be used in other contexts? Let’s take a look at some of them.
Mule application
When you are developing APIs or integrations, you can use DataWeave to transform your data to match the project’s requirements. While developing a Mule application, you can use DataWeave via a Transform Message component like the following:
When you open this component in Anypoint Studio (MuleSoft’s main IDE), you will be able to use the powerful graphical user interface to create quick transformations of your data. This component automatically detects the data structure from the input and the output, and provides this structure in a graphical way that you can use to develop your DataWeave scripts.
You can use this component’s preview option to see how your transformation is doing with the data. However, it may be a little bit time-consuming if you want to try different scenarios before deploying your Mule application. There is a better way to do quick tests of your code, and that is using the DataWeave Playground. Let’s talk about this tool next.
DataWeave Playground
You can access the DataWeave Playground directly from your web browser. This is an excellent way to learn and experiment with different DataWeave scripts using a more lightweight solution.
Notice that at the top-right corner, there’s a button to access the interactive tutorial. If you’re new to DataWeave, this is a great way to get started and test your scripts directly from the browser!
But what if you’re an expert in DataWeave and are looking for more advanced tools to be able to debug, create DataWeave libraries, or test your code? In that case, you’ll love the next point.
Visual Studio Code extension
If you’re a seasoned developer, you might be looking for a more code-driven IDE that offers more options to create complex DataWeave scripts. There is a DataWeave extension for Visual Studio Code that is currently in Beta, but is ready to handle all your DataWeave coding.
With this extension, you will have all the functionalities from the DataWeave Playground plus a bunch of other features, such as extracting variables, debugging, docs auto-generation, tests, dependency management, and more.
What if you want to be able to run DataWeave outside MuleSoft? Let’s move on to the next two points.
DataWeave in Apex
If you’re a Salesforce Developer and want to make use of DataWeave for your applications, you can use DataWeave in Apex (Beta as of Summer ‘23). There’s no need to create a Mule application or do some weird workarounds to make this happen. You can find some examples and the basic setup right from this repository.
Here’s an example of a “Hello World” test in Apex, calling a DataWeave script:
Or if you want to see a more real-life example, check out this script that MuleSoft Ambassador Edgar Moran created in his blog post.
Cool, eh? What if you’re not a MuleSoft or Salesforce Developer and you still want to use DataWeave somehow? Let’s go to our final point.
DataWeave CLI
You can use a DataWeave CLI (command-line interface) to run your transformations from other applications. For example, you could call this tool from a Python or a Bash script to make use of this powerful language without leaving your ongoing project.
Here’s an example:
This tool is also useful for those huge transformations that may not be running so smoothly from any of the previously mentioned tools.
Conclusion
We just went through a list of tools to use DataWeave for different scenarios, from a Mule application with drag-and-drop to a command-line tool. In summary, this is how you can take advantage of these tools:
- In a Mule application: Create APIs and integrations that are capable of transforming the data as needed. You can make use of the visual drag-and-drop feature in Anypoint Studio to create DataWeave scripts.
- In the DataWeave Playground: Experiment with a lightweight solution right from your browser and learn DataWeave with the interactive tutorial.
- In Visual Studio Code with the DataWeave extension: Get a more developer-friendly environment to debug, test, and improve your scripts in less time.
- With DataWeave in Apex: Create transformations with less code right from Apex for your Salesforce solutions.
- With the DataWeave CLI: Create scripts to call directly from your command line or from your favorite programming language.
To learn more about these tools and other DataWeave resources, you can visit the DataWeave landing page.
Further resources
- Testing DataWeave in Apex by Edgar Moran
- The DataWeave CLI by Joshua Erney
- Install DataWeave CLI on Windows by Pranav Davar
- Get Started with DataWeave by Manik Magar
- How to generate shareable link examples from GitHub to open in the DataWeave Playground by Alex Martinez
- How to move your code from the DataWeave Playground to Visual Studio Code by Alex Martinez
- Learn DataWeave with the Online DataWeave Playground and Interactive Tutorial
- Getting started with the DataWeave extension for Visual Studio Code
About the author
Alex Martinez is a Developer Advocate at MuleSoft. She was part of the MuleSoft Community before joining MuleSoft. Alex founded ProstDev to help other professionals learn more about content creation. In her free time, you will find Alex playing Nintendo or Playstation games and writing reviews about them!