Unraveling Data Challenges with DataWeave Part 4: Working with Nested Structures

Antonios Malliotis
Another Integration Blog
2 min readJul 19, 2023

Introduction

Welcome back to our series, “Unraveling Data Challenges with DataWeave”. In Part 4, we dive into the realm of nested data structures and explore how DataWeave can help us manage and manipulate complex, hierarchical data.

Scenario

We’re dealing with JSON data representing a school. Each school has different classes, each class has students, and each student has attributes like name, age, and grade.

{
"school": "Riverdale High",
"classes": [
{
"className": "Class A",
"students": [
{"name": "Alice", "age": 15, "grade": 85},
{"name": "Bob", "age": 14, "grade": 90}
]
},
{
"className": "Class B",
"students": [
{"name": "Charlie", "age": 15, "grade": 92},
{"name": "Dave", "age": 14, "grade": 88}
]
}
]
}

The challenge is to transform this data into a flattened structure where we can easily see which class each student belongs to.

DataWeave Solution

Here’s a DataWeave script that accomplishes this:

%dw 2.0
output application/json
---
payload.classes flatMap (class) -> class.students map (student) -> {
className: class.className,
studentName: student.name,
age: student.age,
grade: student.grade
}

Output

[
{
"className": "Class A",
"studentName": "Alice",
"age": 15,
"grade": 85
},
{
"className": "Class A",
"studentName": "Bob",
"age": 14,
"grade": 90
},
{
"className": "Class B",
"studentName": "Charlie",
"age": 15,
"grade": 92
},
{
"className": "Class B",
"studentName": "Dave",
"age": 14,
"grade": 88
}
]
DataWeave Playground

Conclusion

As we’ve demonstrated, DataWeave makes it easy to work with complex, nested data structures. The ability to quickly flatten nested data is an invaluable tool when dealing with real-world data. Join us next time as we continue to explore more ways to handle common data challenges with DataWeave. Happy data weaving!

--

--