Loading
Prepare for Email to Become the Default Login ExperienceRead More
The online Trailblazer Community becomes read-only Oct 30, 2026Read More
Important Advisory: iOS Update Impacting CG Mobile ApplicationRead More

How to use recursion in DataWeave

Publish Date: Jul 24, 2025
Task

GOAL

To apply recursion to go over a data structure in DataWeave

Steps
If we have an object in DataWeave, composed by other objects and arrays we can go over the object structure recursively. In this example, we want to apply a function that we receive in a parameter to format the string values in the object. We are using the function upper that returns the provided string in uppercase characters:
 
%dw 1.0
%output application/json

%var data = {
    "VersionNo": "1.1",
    "Program": "points program xx",
    "Items": [
         {
            "ID": "456",
            "Field1": 5,
            "Field2": "xy"
         },
         {
            "ID": "123",
            "Field1": 2,
            "Field2": "yz"
        }
    ]
}

%function formatAllValues (input, formatter)
    input match {
        :array -> input map (value,index) -> formatAllValues(value, formatter),
        :object -> input mapObject (value,key) -> {
            (key) : formatAllValues (value, formatter)
        },
        :string -> formatter(input),
        default -> input
    }

---

formatAllValues(data, (str) -> upper str)
Note the highlighted code:
  • The formatAllValues function receives as parameters a data structure and a lambda
  • It uses the the match operator to decide between the possible types of the received structure
  • If the type is an array, it calls recursively to each element of the array
  • If it is an object, it calls recursively to each property of the object
  • For the base cases, if the data is a string it applies the received lambda, otherwise it returns the received value.

 
Knowledge Article Number

001115008

 
Loading
Salesforce Help | Article