Understanding Recursive Function Usage in DataWeave to Repeat JSON Objects

Nitin Prakash
3 min readJul 26, 2023

--

Introduction:

DataWeave, a powerful data transformation language, offers a range of functions to manipulate and transform data. Among these, recursive functions are an essential concept that allows developers to perform repetitive operations in an elegant and concise manner. In this article, we will explore how to utilize a recursive function in DataWeave to repeat a JSON object or array based on a specified size.

Understanding the DataWeave Script:

Let’s discuss the provided DataWeave script to understand how it leverages a recursive function to repeat a JSON object based on the size passed as input.

Dataweave:

%dw 2.0
output application/json

// Step 1: Define the desired size for repetition
var size = 3

// Step 2: Define a recursive function to repeat the input data
fun inputRep(pay, counter) = if (counter < size) pay ++ inputRep(pay, (counter + 1)) else payload

// Step 3: Initialize a counter variable
var counter = 1

// Step 4: Call the recursive function with the payload and counter
---
inputRep(payload, counter)

Explanation of the Steps:

In the first step, a variable size is defined and assigned the value 3. This variable determines how many times the input JSON object should be repeated.

The script then defines a recursive function named inputRep. A recursive function is a function that calls itself within its definition. In this case, the function takes two parameters: pay (representing the input data) and counter (representing the number of repetitions). The function works as follows:

a. If the counter is less than the specified size, it concatenates (++) the pay variable with the result of calling the inputRep function again, incrementing the counter by 1 in each recursive call.

b. If the counter is equal to or greater than the specified size, it returns the original payload, effectively stopping the recursion.

Next, the script initializes a counter variable and sets its value to 1. The counter variable will keep track of the number of repetitions.

Finally, the recursive function inputRep is called with the initial payload and the counter. The result of this function call is the output of the entire DataWeave script.

Example Input:
Let’s consider the following input JSON:

[{
"message": "Hello world!",
"message1" : "new Hello"
}]

Output Explanation:

The provided input is a JSON array with a single object containing two key-value pairs. Since the specified size in the script is 3, the output will be a JSON array containing the same input object repeated three times. The recursive function inputRep will concatenate the input JSON with itself two more times to achieve the repetition.

[
{
"message": "Hello world!",
"message1": "new Hello"
},
{
"message": "Hello world!",
"message1": "new Hello"
},
{
"message": "Hello world!",
"message1": "new Hello"
}
]

Github link: https://github.com/nitinprakash05/mulesoft/blob/dataweave/repeatJsonObject

Conclusion:

In this article, we explored the concept of recursive functions in DataWeave by using an example script that repeats a JSON object or array based on a specified size. The recursive function inputRep allowed us to achieve this repetition effortlessly, showcasing the flexibility and power of DataWeave in data transformation tasks. Developers can adapt this approach to repeat data structures based on varying requirements, thereby streamlining data manipulation processes.

--

--