Mastering DataWeave: Simplifying Complex JSON Structures | MuleSoft Guide

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

  1. Grouping by Parent ID:
    • We begin by grouping the input data using the groupBy function based on the parentId field.
    • Orders without a parent are assigned to a default group named “parentorders.”
  2. Creating Child Items:
    • For each parent order, a new field named chailItems is created.
    • This field is populated with the corresponding child items (if any) based on the parentId.
  3. Handling Empty Child Items:
    • If an order has no associated child items, the chailItems field is omitted from the output.

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 groupBy function groups the input data based on the parentId field.
  • The default clause ensures that orders without a parent are grouped under “parentorders.”
  • For each parent order, the chailItems field is created and populated with the corresponding child items (if any).
  • The if(!isEmpty(chaild)) condition ensures that the chailItems field 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.

]]>
Post a Comment (0)
Previous Post Next Post