DataWeave Tutorial: Transform Nested JSON Structures | MuleSoft Guide

DataWeave, a powerful transformation language used in MuleSoft, allows developers to manipulate and convert data between different formats. In this article, we’ll dive into a practical example of using DataWeave to transform a complex nested structure. Our goal is to make this topic accessible to beginners while ensuring that the content remains unique and engaging.

Understanding the Problem

Suppose we have an input JSON array representing orders and related information. Each order can have an associated parent order, and some orders include additional details. Our task is to transform this input into a more structured output format.

Input Data

[
{
"oderId": 1,
"orderName": "test_name_0",
"qty": 1
},
{
"oderId": 2,
"orderName": "test_name_1",
"qty": 1
},
{
"oderId": 3,
"orderName": "test_name_2",
"qty": 2
},
{
"oderId": 4,
"orderName": "test_name_3",
"qty": 3
},
{
"oderId": 5,
"orderName": "test_name_4",
"qty": 4
},
{
"parentId": 2,
"address": "test_address_3",
"contact": "9449683520"
},
{
"parentId": 3,
"address": "test_address_7",
"contact": "9658659348"
},
{
"parentId": 4,
"address": "test_address_13",
"contact": "9640593598"
}
]

Desired Output

[
{
"oderId": 1,
"orderName": "test_name_0",
"qty": 1
},
{
"oderId": 2,
"orderName": "test_name_1",
"qty": 1,
"chailItems": [
{
"parentId": 2,
"address": "test_address_3",
"contact": "9449683520"
}
]
},
{
"oderId": 3,
"orderName": "test_name_2",
"qty": 2,
"chailItems": [
{
"parentId": 3,
"address": "test_address_7",
"contact": "9658659348"
}
]
},
{
"oderId": 4,
"orderName": "test_name_3",
"qty": 3,
"chailItems": [
{
"parentId": 4,
"address": "test_address_13",
"contact": "9640593598"
}
]
},
{
"oderId": 5,
"orderName": "test_name_4",
"qty": 4
}
]

Breaking Down the Solution

Grouping by Parent ID:

We start by grouping the input data by the parentId field. This allows us to associate child items with their respective parent orders.

If an order doesn’t have a parent, we assign it to a special group called parentorders.

Creating Child Items:

For each parent order, we create a new field called chailItems.

We populate this field with the related child items (if any) based on the parentId.

Handling Empty Child Items:

If an order has no child items, we omit the chailItems field altogether.

DataWeave Script

%dw 2.0
var data = payload groupBy ((item, index) -> item.parentId default "parentorders")
output application/json
---
data.parentorders map (do {
var chaild = data["$($.oderId)"]
---
$ ++ {
(chailItems: chaild) if (!isEmpty(chaild))
}
})

Explanation

We use the groupBy function to group the input data by parentId. The default clause ensures that orders without a parent are grouped under “parentorders“.

For each parent order, we create a new field called chailItems. We populate it with the related child items (if any) based on the parentId.

We handle cases where an order has no child items by omitting the chailItems field.

Conclusion

DataWeave is a versatile tool for transforming data structures, and understanding its concepts is essential for MuleSoft developers. By breaking down complex problems and providing clear explanations, we hope this article helps beginners grasp the power of DataWeave.

Post a Comment (0)
Previous Post Next Post