DataWeave is a powerful transformation language used in MuleSoft for data integration. With DataWeave, you can easily transform data between different formats and structures. In this blog, we will explore some common data transformation scenarios in DataWeave and provide examples to illustrate each scenario.
Mapping Data between Different APIs
One of the most common use cases for DataWeave is to map data between different APIs. This can involve transforming data from one API’s format to another, or merging data from multiple APIs into a single output. For example, let’s say you have two APIs that provide weather data in different formats. You can use DataWeave to transform the data from both APIs into a common format, like JSON, that can be easily consumed by your application.
Example:
%dw 2.0
output application/json
---
{
city: payload.name,
temperature: payload.main.temp,
humidity: payload.main.humidity,
wind: {
speed: payload.wind.speed,
direction: payload.wind.deg
}
}
In this example, we are transforming weather data from an API that provides data in the OpenWeatherMap format to a JSON format that can be consumed by our application.
Normalizing Data
Data normalization involves converting data from one format to another to ensure consistency and accuracy. This is especially important when dealing with data from multiple sources that may have different structures or formats. With DataWeave, you can easily normalize data by mapping fields from different sources to a common format.
Example:
Let’s say you have two APIs that provide customer data, but each API has a different structure. You can use DataWeave to normalize the data into a common format, like the following:
%dw 2.0
output application/json
---
{
firstName: payload.firstName,
lastName: payload.lastName,
email: payload.email,
phone: payload.phone,
address: {
street: payload.address.line1,
city: payload.address.city,
state: payload.address.state,
zip: payload.address.zip
}
}
In this example, we are normalizing customer data from two different APIs into a common JSON format that can be used by our application.
Aggregating Data
Data aggregation involves combining data from multiple sources into a single output. This is useful when you need to create a unified view of data from different sources. With DataWeave, you can easily aggregate data by mapping fields from different sources to a common output.
Example:
Let’s say you have two APIs that provide sales data for different regions. You can use DataWeave to aggregate the data into a single output that shows sales data for all regions.
%dw 2.0
output application/json
---
{
region1: payload[0].sales,
region2: payload[1].sales,
region3: payload[2].sales,
totalSales: sum(payload.*.sales)
}
In this example, we are aggregating sales data from three different regions into a single output that shows total sales and sales data for each region.
Filtering Data
Data filtering involves selecting specific data from a source based on a set of criteria. With DataWeave, you can easily filter data by using conditional statements to select data that meets certain conditions.
Example:
Let’s say you have an API that provides product data, but you only want to select products that are in stock. You can use DataWeave to filter the data based on the inventory status.
%dw 2.0 output application/json --- payload filter $.inventoryStatus == "in stock"
In this example, we are filtering product data based on the inventory status field