DataWeave x Apex: Data Transformation Made Easy

Have you write ever had to parse an XML or CSV in Apex? Me too! And we both know how awful that is!!

Well, lucky us. Dataweave in Apex will be in beta in Spring ’23. And yes, you will be able to play around with it in your org (Enterprise, Performance, Unlimited, and Developer editions) free of cost!!

But wait..

What is DataWeave?

Simply put, DataWeave is a programming language designed for transforming data, meaning converting data from one format to another. For example, CSV ↔ JSON or XML ↔ JSON. You can learn more about DataWeave here. It’s an easy and fun language once you get the basics right.

With DW, you can do data transformations very easily, which may seem very complex in Apex. Sometimes it’s as easy writing one line of code, no kidding!

Now that I’ve your attention…

How do I get my hands dirty?

Let me walk you through a small tutorial explaining how you can use DW in Apex in your org.

Step 1: You need a Spring ’23 org. If you already have one, great! If not, you can sign up for one.

Step 2: You will need VS code (or you can simply use SFDX) to deploy the DW files. I’ll use VS code for the example because… why not!

Step 3: Create a new Project in your VS Code and authorize your Spring ’23 org.

Step 4: Create a new folder in force-app\main\default and name it ‘dw’.

Step 5: Create 2 files in ‘dw’ folder and name them xmlToJson.dwl and xmlToJson.dwl-meta.xml respectively.

Step 6: Copy the following code in the xmlToJson.dwl-meta.xml file

<?xml version="1.0" encoding="UTF-8"?>
<DataWeaveResource xmlns="">

Step 7: Now let’s write some DW in the xmlToJson.dwl file.

This script converts XML to JSON array.

%dw 2.0

/* Here you can define your inputs and output here. */

input data application/xml      // 'data' here acts as a variable that will be holding the incoming data.
output application/json

/* Write your script here */

    "contacts" : (
        data.contacts.*contact map { 
            "LastName": $.LastName,
            "Email": $.Email

Step 8: Deploy the xmlToJson.dwl file to org.

Step 9: Let’s see how we can call this DW script in Apex.

String xmlData= '<contacts> <contact> <Name>John</Name> <LastName>Doe</LastName> <Email></Email> </contact> <contact> <Name>Jane</Name> <LastName>Doe</LastName> <Email></Email> </contact> </contacts>';

DataWeave.Script dwscript = DataWeave.Script.createScript('xmlToJson'); //xmlToJson is the name of DW file.
DataWeave.Result dwresult = dwscript.execute(new Map<String, Object>{
    'data' => xmlData
}); //Execute method takes in a map of input parameters.


Output (Executing Apex Anonymously):

So simple, yes!? Imagine having to do this in Apex. 🤢
Similarly, you can also CSV to SObject List with just a few lines of code. DataWeave has a lot to offer!

Hope you find this one useful! Catch you in the next one! ✌
And thank you for being an awesome reader! Subscribe to this blog for receiving all the latest updates straight to your inbox. 🙂

2 thoughts on “DataWeave x Apex: Data Transformation Made Easy

  1. The next thing is to create the Script dynamically based on the Object or from the file to match an object. That way, the import can be completely dynamic.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.