Sitemap

DataWeave Practice Questions with Answers Section 31–40

3 min readSep 11, 2025

--

31. Convert Numbers to Strings with Prefix

Question: Convert all numbers into strings prefixed with "ID-".

[101, 102, 103]

Answer:

%dw 2.0
output application/json
---
payload map (num) -> "ID-" ++ num as String

Output:

["ID-101", "ID-102", "ID-103"]

32. Extract Keys from JSON Object

Question: Extract all keys from the following JSON object.

{
"name": "Ravi",
"age": 25,
"city": "Delhi"
}

Answer:

%dw 2.0
output application/json
---
keysOf(payload)

Output:

["name", "age", "city"]

33. Split and Transform String

Question: Split the string "apple,banana,grapes" into an array and capitalize each element.

Get Mule4Developer’s stories in your inbox

Join Medium for free to get updates from this writer.

Answer:

%dw 2.0
output application/json
---
("apple,banana,grapes" splitBy ",") map (item) -> upper(item)

Output:

["APPLE", "BANANA", "GRAPES"]

34. Sum of Even Numbers Only

Question: From the array, calculate the sum of only even numbers.

[1, 2, 3, 4, 5, 6]

Answer:

%dw 2.0
output application/json
---
sum(payload filter (x) -> x mod 2 == 0)

Output:

12

35. Convert JSON Array into CSV

Question: Convert the following JSON array into CSV format.

[
{"id": 1, "name": "Ritu"},
{"id": 2, "name": "Aman"}
]

Answer:

%dw 2.0
output text/csv
---
payload

Output:

id,name
1,Ritu
2,Aman

36. Multi-Level Grouping

Question: Group employees by department and then by role.

[
{"name": "Ajay", "dept": "IT", "role": "Developer"},
{"name": "Sunita", "dept": "IT", "role": "Tester"},
{"name": "Ravi", "dept": "HR", "role": "Recruiter"},
{"name": "Neha", "dept": "IT", "role": "Developer"}
]

Answer:

%dw 2.0
output application/json
---
(payload groupBy ((e) -> e.dept)) mapObject ((emps, dept) ->
(dept): (emps groupBy ((x) -> x.role))
)

Output:

{
"IT": {
"Developer": [
{"name": "Ajay", "dept": "IT", "role": "Developer"},
{"name": "Neha", "dept": "IT", "role": "Developer"}
],
"Tester": [
{"name": "Sunita", "dept": "IT", "role": "Tester"}
]
},
"HR": {
"Recruiter": [
{"name": "Ravi", "dept": "HR", "role": "Recruiter"}
]
}
}

37. Transform JSON into Key-Value Pairs with Nested Objects

Question: Transform the array into an object where each student’s name is the key and their marks object is the value.

[
{"name": "Ravi", "marks": {"math": 80, "science": 90}},
{"name": "Sneha", "marks": {"math": 85, "science": 95}}
]

Answer:

%dw 2.0
output application/json
---
(payload map (s) -> {(s.name): s.marks}) reduce ((a,b) -> a ++ b)

Output:

{
"Ravi": {"math": 80, "science": 90},
"Sneha": {"math": 85, "science": 95}
}

38. Complex Transformation with Calculated Field

Question: Add a new field "taxAmount" (18% of "price") to each product.

[
{"id": 1, "price": 1000},
{"id": 2, "price": 2000}
]

Answer:

%dw 2.0
output application/json
---
payload map (p) -> p ++ { taxAmount: p.price * 0.18 }

Output:

[
{"id": 1, "price": 1000, "taxAmount": 180},
{"id": 2, "price": 2000, "taxAmount": 360}
]

39. Restructure JSON into Nested Hierarchy

Question: Transform flat employee list into department-wise employees.

[
{"empId": 1, "name": "Arun", "dept": "Finance"},
{"empId": 2, "name": "Meera", "dept": "Finance"},
{"empId": 3, "name": "Kiran", "dept": "IT"}
]

Answer:

%dw 2.0
output application/json
---
(payload groupBy ((e) -> e.dept)) mapObject ((value, key) ->
(key): { employees: value }
)

Output:

{
"Finance": {
"employees": [
{"empId": 1, "name": "Arun", "dept": "Finance"},
{"empId": 2, "name": "Meera", "dept": "Finance"}
]
},
"IT": {
"employees": [
{"empId": 3, "name": "Kiran", "dept": "IT"}
]
}
}

40. Multi-Source Merge with Derived Value

Question: Merge student details with exam scores and add a new field "status":

  • "Pass" if marks ≥ 40
  • "Fail" otherwise

Students:

[
{"id": 1, "name": "Raj"},
{"id": 2, "name": "Pooja"}
]

Scores:

[
{"id": 1, "marks": 55},
{"id": 2, "marks": 30}
]

Answer:

%dw 2.0
output application/json
var students = payload.students
var scores = payload.scores
---
students map (s) ->
s ++ (scores filter (sc) -> sc.id == s.id)[0]
++ { status: if ((scores filter (sc) -> sc.id == s.id)[0].marks >= 40) "Pass" else "Fail" }

Output:

[
{"id": 1, "name": "Raj", "marks": 55, "status": "Pass"},
{"id": 2, "name": "Pooja", "marks": 30, "status": "Fail"}
]

--

--