How to Extract All Values from a JSON Input in DataWeave 2.0

Sandeep Sai Kumar Kancharla
Another Integration Blog
3 min readMay 2, 2023

DataWeave 2.0 is a powerful language for transforming data. It has many features and functions that make it easy to manipulate and process different types of data. In this blog post, I will show you how to write a simple DataWeave script that can extract all the values from a JSON input and return them as a flat array.

The Problem

Suppose you have a JSON input that looks like this:

{
"name": "John",
"age": 25,
"address": {
"street": "123 Main St",
"city": "New York",
"zip": 10001
},
"hobbies": ["reading", "writing", "coding"]
}

You want to write a DataWeave script that can take this input and return an array of all the values from it, like this:

["John", 25, "123 Main St", "New York", 10001, "reading", "writing", "coding"]

How can you do that?

The Solution

The solution is to write a DataWeave script that defines three functions: getValues, getValuesFromArray and getValuesFromObject. These functions will use pattern matching and flatMap to recursively extract all the values from the input and flatten them into a single array.

Let’s see how these functions work.

The getValues Function

The getValues function takes any JSON value and returns an array of all the values from it. It uses pattern matching to check if the input is an object, an array or something else. If it is an object, it calls the getValuesFromObject function. If it is an array, it calls the getValuesFromArray function. Otherwise, it returns the input as it is.

Here is the code for the getValues function:

fun getValues(inBound) = (
inBound match {
case is Object -> getValuesFromObject(inBound)
case is Array -> getValuesFromArray(inBound)
else -> inBound
}
)

For example

  1. if we call getValues(42), it will return 42. If we call getValues([1, 2, 3]).
  2. it will call getValuesFromArray([1, 2, 3]).
  3. If we call getValues({“a”: 1, “b”: 2}), it will call getValuesFromObject({“a”: 1, “b”: 2}).

The getValuesFromArray Function

The getValuesFromArray function takes an array and returns an array of all the values from it. It uses flatMap to apply the getValues function to each element of the array and flatten the result.

Here is the code for the getValuesFromArray function:

fun getValuesFromArray(list :Array) = (
list flatMap(
$ match {
case is Object -> getValuesFromObject($)
case is Array -> getValuesFromArray($)
else -> $
}
)
)

For example, if we call getValuesFromArray([1, [2, [3]], {“a”: 4}]), it will return [1, 2, 3, 4].

The getValuesFromObject Function

The getValuesFromObject function takes an object and returns an array of all the values from it. It uses valuesOf to get an array of the object’s values and then uses flatMap to apply the getValues function to each element of the array and flatten the result.

Here is the code for the getValuesFromObject function:

fun getValuesFromObject(inBound :Object) = (
do{
var values = valuesOf(inBound)
---
values flatMap(
$ match {
case is Object -> getValuesFromObject($)
case is Array -> getValuesFromArray($)
else -> $
}
)
}
)

For example, if we call getValuesFromObject({“a”: [1], “b”: {“c”: [2]}}), it will return [1, 2].

The Final Script

The final script is very simple. It just calls the getValues function on the payload, which is a predefined variable that holds the input data. The output is a JSON array of all the values from the payload.

Here is the final script:

%dw 2.0
output application/json

fun getValues(inBound) = (
inBound match {
case is Object -> getValuesFromObject(inBound)
case is Array -> getValuesFromArray(inBound)
else -> inBound
}
)

fun getValuesFromArray(list :Array) = (
list flatMap(
$ match {
case is Object -> getValuesFromObject($)
case is Array -> getValuesFromArray($)
else -> $
}
)
)

fun getValuesFromObject(inBound :Object) = (
do{
var values = valuesOf(inBound)
---
values flatMap(
$ match {
case is Object -> getValuesFromObject($)
case is Array -> getValuesFromArray($)
else -> $
}
)
}
)
---
getValues(payload)

for above given input

Conclusion

In this blog post, I showed you how to write a simple DataWeave script that can extract all the values from a JSON input and return them as a flat array. I hope you found this useful and learned something new about DataWeave.

If you have any questions or feedback, please leave a comment below. Thank you for reading!

--

--