10 Essential DataWeave Examples to Master Your MuleSoft Transformations (Part I)

Welcome to our "Preparation Solutions" series! If you work with MuleSoft, you know that DataWeave is the powerful engine that keeps your integrations moving. It transforms data from one format to another, like translating a language instantly.


However, you don't always need to build a complex engine from scratch. Sometimes, you just need a quick "recipe" to solve a specific data problem. Below, we have compiled 10 common transformation scenarios. To demonstrate the flexibility of DataWeave, we have provided multiple script solutions for many of these scenarios. Whether you prefer using map, filter, or reduce, we have you covered.

Let’s dive into the code.


1. Merging Arrays into a Single Object

The Goal: You have an array containing multiple separate objects (e.g., an ID in one object, a message in another). You need to flatten this list into a single, unified object.

Input Data

[
   {"id": 123,"name": "sample name"
   },
   {"message": "Hello world!"
   }
]

Target Output

{"id": 123,"name": "sample name","message": "Hello world!"
}

Solutions

Option 1: Using the dynamic object constructor
This method dynamically unpacks the payload into key-value pairs.

%dw 2.0
output application/json
---
{(payload)}

Option 2: Using reduce
The reduce function iterates over the array, merging (++) each item into the accumulator.

%dw 2.0
output application/json
---
payload reduce ((item, accumulator) -> accumulator ++ item )

2. Extract Specific Fields (Field Filtering)

The Goal: You need to grab only the id and name from a payload, effectively removing the message field. The challenge here is to do it without defining a manual one-to-one mapping for every field.

Input Data

[
   {"id": 123,"name": "sample name","message": "Hello world!"
   }
]

Target Output

{"id": 123,"name": "sample name"
}

Solutions

There are many ways to skin this cat. Here are 6 different approaches ranging from filtering objects to subtraction:

%dw 2.0
output application/json
---
payload[0] filterObject ((value, key, index) -> !(key ~= "message"))

%dw 2.0
output application/json
---
(payload map{
   id : $.id,
   name : $.name
})[0]

%dw 2.0
output application/json
---
payload[0] mapObject ((value, key, index) ->
((key) : value) if !(key ~= "message")
)

%dw 2.0
output application/json
---
payload[0] - "message"

%dw 2.0
output application/json
---
(payload map(
   $ - "message"
))[0]

%dw 2.0
output application/json
---
(payload map(
   $ filterObject ($$ ~= "id" or $$ ~= "name")
))[0]

3. Filtering by Reference Array

The Goal: You have a list of input numbers. You only want to keep the numbers that also exist in a specific reference array [1,3,4,5].

Input Data

[ 1, 2, 3, 4 ]

Target Output

[ 1, 3, 4 ]

Solutions

%dw 2.0
output application/json
import * from dw::core::Arrays
var tmpArray :Array = [1,3,4,5]
---
join(payload,tmpArray,(pldItem)->pldItem,(tmpItem)->tmpItem).l

%dw 2.0
output application/json
import * from dw::core::Arrays
var tmpArray :Array = [1,3,4,5]
---
payload filter(tmpArray contains $)

%dw 2.0
output application/json
import * from dw::core::Arrays
var tmpArray :Array = [1,3,4,5]
---
payload -- (payload -- tmpArray)

%dw 2.0
output application/json
import * from dw::core::Arrays
var tmpArray :Array = [1,3,4,5]
---
payload flatMap(
   (tmpArray groupBy ($) )["$($)"]
) filter(!isEmpty($))

4. Calculating Date Differences

The Goal: Simply find the number of days between two specific timestamps.

Input Data

["2022-10-01T23:57:59-03:00", "2022-11-01T23:57:59-03:00"]

Target Output

31

Solutions

%dw 2.0
output application/json
---
daysBetween(payload[0],payload[1])

%dw 2.0
output application/json
import * from dw::core::Periods
---
((payload[1] - payload[0]) as Period).days

%dw 2.0
output application/json
---
(payload[1] as DateTime - payload[0] as DateTime).days

5. String Suffix Filtering

The Goal: Filter a list of names to return only those that end with the letter "i".

Input Data

["vamsi","hari","ravi","sandy","kumar"
]

Target Output

["vamsi","hari","ravi"
]

Solutions

%dw 2.0
output application/json
---
payload filter ((item, index) -> item endsWith  "i")

%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload takeWhile ((item) -> item endsWith  "i")

%dw 2.0
output application/json
import * from dw::core::Arrays
---
(payload partition ((item) -> item endsWith  "i")).success

%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload -- (payload dropWhile ((item) -> (item endsWith  "i")))

%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload filter($[-1] ~= "i")

6. Extracting Uppercase Keys

The Goal: Scan an object and return only the key-value pairs where the Key is fully Uppercase.

Input Data

{"A": 1324,"b": 1432,"v": 9237,"D": 24365,"R": 213254
}

Target Output

{"A": 1324,"D": 24365,"R": 213254
}

Solutions

%dw 2.0
output application/json
---
payload filterObject ((value, key, index) -> upper(key) ~= key)


%dw 2.0
output application/json
---
payload filterObject ((value, key, index) -> !(key matches /[a-z]/))

7. Find Index by Value

The Goal: Find the index positions of any key that holds the numeric value 123.

Input Data

{"id": 123,"sampleid": "123","id": 123,"name": "sample name"
}

Target Output

[0,2
]

Solutions

%dw 2.0
output application/json
---
(payload mapObject ((value, key, index) ->
("value" : index) if(value == 123)
)).*value

%dw 2.0
output application/json skipNullOn="everywhere"
---
payload pluck(
if($ == 123)$$$ else null
)

%dw 2.0
output application/json
---
payload pluck(
if($ == 123)$$$ else null
) filter(!isEmpty($))

8. Flattening and Extracting Decimals

The Goal: You have a nested array (arrays within an array). You need to extract only the float (decimal) values.

Input Data

[
 [3,5
 ],
 [0.9,5.5
 ]
]

Target Output

[0.9,5.5
]

Solutions

%dw 2.0
output application/json
---
flatten(payload) filter(
   isDecimal($)
)

%dw 2.0
output application/json
---
payload flatMap(
   $ filter(isDecimal($))
)

9. Extracting Object Keys

The Goal: Simply get a list of all the Keys found in the input object.

Input Data

{"id": 123,"sampleid": 123,"id": 123,"name": "sample name"
}

Target Output

["id","sampleid","id","name"
]

Solutions

%dw 2.0
output application/json
---
keysOf(payload)

%dw 2.0
output application/json
import * from dw::core::Objects
---
keySet(payload)

%dw 2.0
output application/json
import * from dw::core::Objects
---
nameSet(payload)

10. JSON to XML Conversion

The Goal: Convert a complex JSON menu structure into valid XML, ensuring the hierarchy is preserved correctly.

Input Data

[
 {"menu": {"id": "file","value": "File","popup": {"menuitem": [
         {"value": "New","onclick": "CreateNewDoc()"
         },
         {"value": "Open","onclick": "OpenDoc()"
         },
         {"value": "Close","onclick": "CloseDoc()"
         }
       ]
     }
   }
 }
]

Target Output

<?xml version='1.0' encoding='UTF-8'?>
<menusList>
  <menus>
    <menu>
      <id>file</id>
      <value>File</value>
      <popup>
        <menuitem>
          <value>New</value>
          <onclick>CreateNewDoc()</onclick>
        </menuitem>
        <menuitem>
          <value>Open</value>
          <onclick>OpenDoc()</onclick>
        </menuitem>
        <menuitem>
          <value>Close</value>
          <onclick>CloseDoc()</onclick>
        </menuitem>
      </popup>
    </menu>
  </menus>
</menusList>

Solutions

%dw 2.0
output application/xml
---
menusList : menus: {(payload)}
%dw 2.0
output application/xml
---
menusList : menus: (payload reduce($$ ++ $))

Conclusion

DataWeave is incredibly versatile. As seen above, there is rarely just one way to solve a problem. Whether you are filtering data, transforming formats, or performing calculations, mastering these basic patterns will make your MuleSoft integrations faster and more reliable.

Post a Comment (0)
Previous Post Next Post