Efficiently Transform JSON with DataWeave | MuleSoft Guide

Problem:

  • Updating individual properties within deeply nested JSON objects using the update function can be inefficient for large datasets.
  • The update function may traverse the entire object tree to locate a specific property, leading to performance issues.

Solution:

  • Leverage DataWeave operators like map, filter, and reduce to efficiently transform JSON.
  • These operators often provide better performance for large datasets due to their simpler logic and easier debugging.
  • Break down complex transformations into smaller, more manageable steps.

Implementation:

This DataWeave 2.0 script demonstrates how to reformat JSON keys:

%dw 2.0output application/json// Function to recursively reformat keysfun formatKeys(inBound, fromSymbol: String, toSymbol: String) =     inBound match {        case is Array -> inBound map (formatKeys($, fromSymbol, toSymbol))        case is Object -> inBound mapObject (            (value, key) -> (key replace "$(fromSymbol)" with "$(toSymbol)") ++ value        )        else -> inBound    }// Example usage: Replace underscores with hyphensformatKeys(payload, "_", "-")

Explanation:

  1. formatKeys function:
    • Recursively handles arrays and objects.
    • For arrays, it applies formatKeys to each element.
    • For objects, it uses mapObject to iterate through key-value pairs:
      • Replaces fromSymbol with toSymbol in the key.
      • Maintains the original value.
  2. Example usage:
    • Calls formatKeys with payload, _ (from symbol), and - (to symbol).
    • This effectively replaces all underscores in keys with hyphens.

Benefits:

  • Improved performance: Efficiently handles large datasets compared to the update function.
  • Clear and concise: Easy to understand and maintain.
  • Flexibility: Easily adaptable to different key replacement scenarios.

Example:

Input:

{  "id": "0001",  "type_1": "donut",  "name": "Cake",  "image": {    "url_link": "images/0001.jpg",    "width": 200,    "height": 200  }}

Output:

{
"id": "0001",
"type-1": "donut",
"name": "Cake",
"image": {
"url-link": "images/0001.jpg",
"width": 200,
"height": 200
}
}
]]>
Post a Comment (0)
Previous Post Next Post