DataWeave, a powerful transformation language within MuleSoft, empowers developers to manipulate and convert data between various formats. This article delves into a practical example of using DataWeave to transform a complex nested JSON structure. Our aim is to make this topic accessible to beginners while ensuring the content remains unique and engaging.
The Challenge
Consider an input JSON array representing orders and their associated information. Each order might have a parent order, and some orders may include additional details. Our objective 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 }]The Solution
- Grouping by Parent ID:
- We begin by grouping the input data using the
groupByfunction based on theparentIdfield. - Orders without a parent are assigned to a default group named “parentorders.”
- We begin by grouping the input data using the
- Creating Child Items:
- For each parent order, a new field named
chailItemsis created. - This field is populated with the corresponding child items (if any) based on the
parentId.
- For each parent order, a new field named
- Handling Empty Child Items:
- If an order has no associated child items, the
chailItemsfield is omitted from the output.
- If an order has no associated child items, the
DataWeave Script:
%dw 2.0output application/jsonvar data = payload groupBy ((item, index) -> item.parentId default "parentorders")---data.parentorders map( do { var chaild = data["$($.oderId)"] --- $ ++ { (chailItems : chaild) if(!isEmpty(chaild)) } })Explanation:
- The
groupByfunction groups the input data based on theparentIdfield. - The
defaultclause ensures that orders without a parent are grouped under “parentorders.” - For each parent order, the
chailItemsfield is created and populated with the corresponding child items (if any). - The
if(!isEmpty(chaild))condition ensures that thechailItemsfield is only included in the output if it contains data.
Conclusion
DataWeave is a versatile tool for transforming data structures within the MuleSoft ecosystem. By understanding its core concepts, developers can efficiently manipulate and convert data between various formats. This article provides a practical example of using DataWeave to transform a nested JSON structure, demonstrating its power and flexibility.
]]>