DataWeave 2.0 Deep Dive – Part 1: Map vs MapObject, Filter vs FilterObject, and Pluck
What is DataWeave?
Imagine you have data coming in different format— like JSON from a mobile app, XML from banking system, or CSV from Excel sheets and you need to rename, reshape, clean, or convert that data before passing it to another system.
That is where DataWeave came into the picture.
DataWeave is MuleSoft’s powerful and concise functional programming language used for data transformation and manipulation.
It is built to handle a wide range of data formats, including:
- JSON
- XML
- CSV
- Plain Text
Should You Learn DataWeave?
In my understanding — A Big yes. If you are working with MuleSoft, learning DWL is not optional it is mandatory.
Here is why:
- most of interview questions in MuleSoft involve DataWeave.
- It allows you to build smart, efficient, and flexible APIs.
- It is a must-have skill if you want to become a certified MuleSoft Developer.
There are many powerful functions in DataWeave, but we will begin by mastering the most essential ones:
- map and mapObject – for transforming arrays and objects
- filter and filterObject – for removing unwanted data
- pluck – for converting objects into arrays with full control
map is used to iterate(the process of doing something again and again) through each item in an array and gives you the flexibility to transform each element. you can add new fields, change values, or reshape the structure.
Key Points:
- Works only on arrays
- Allows adding/modifying fields per element
mapObject is used to iterate through objects (key-value pairs) and allows you to access both the key and the value. You can rename keys, modify values, and even use conditions (if-else) to generate custom fields.
Key Points:
- Works on objects
- Gives access to both key and value
- Can rename keys or filter them
- Supports if-else logic inside transformation
Note: I have shared a hands-on POC of map and mapObject in the form of screenshots below to help you visualize how they work in real scenarios.
filter function is used to iterate through an array and used to remove unwanted data based on a condition. It helps you remove item that do not match your logic.
Key Points:
- Works only on arrays
- Keeps only item that match your condition
filterObject function is used to filter key-value pairs from an object based on a condition. It allows you to apply logic using both the key and the value.
Key Points:
- Works on objects
- Filters based on key and/or value
- Returns a new object with matching key-value pairs only
Note: I have shared a hands-on POC of filter and filterObject in the form of screenshots below to help you visualize how they work in real scenarios
pluck function is used to iterate over an object and convert its key-value pairs into an array. It gives you access to the key, the value, and the index of each item, allowing you to control exactly how each element in the resulting array should look.
Unlike mapObject, which keeps the structure as an object, pluck lets you reshape the data into an array, giving you full control over how each item should look.
Works on objects
- Returns an array of transformed entries
- Gives access to:
- The value (data of each field)
- The key (name of the field)
- The index (position of the entry in the object)
Note: I have shared a hands-on POC of pluck in the form of screenshots below to help you visualize how they work in real scenarios.
Comments
Post a Comment
For more information kindly inbox at yousufbgp@gmail.com