Dataweave Series for Practice & Interview Part-6
Practice Dataweave here :- Dataweave Playground
Important Note:- First Copy Input, Output, DW Code & Use Json Online Validator, to format the JSON, since medium changes the JSON.
- Sum of Numbers Using Recursion
Input:-
[]
Output:-
55
Dataweave Code:-
var arr = 1 to 10
fun recsum(arr, total=0) =
if (not (isEmpty(arr)))
(recsum(arr[1 to -1], total + arr[0]))
else
(total)
- -
recsum(arr)
2. Flatten an Object
{
"item": {
"type": "book",
"price": 49.99,
"properties": {
"title": "XQuery Kick Start",
"author": ["raj","shubh"],
"year": 2003
}
}
}
Output:-
{
"type": "book",
"price": 49.99,
"title": "XQuery Kick Start",
"author": [
"raj",
"shubh"
],
"year": 2003
}
Dataweave Code:-
%dw 2.0
import * from dw::core::Objects
output application/json
fun flattenObject(data)= keysOf(data) reduce (obj,acc={}) ->acc ++ (
if (typeOf(data[obj])~="String" or typeOf(data[obj])~="Number" or typeOf(data[obj])~="Array")
{
(obj):data[obj]
}
else
flattenObject(data[obj])
)
- -
flattenObject(payload)
3. Remove Count Key from each object
Input:-
{
"notification": [{
"status": {
"code": "BILLING",
"value": "Past Due"
},
"count": 1
}],
"notification1": [{
"status": {
"code": "RENEWAL",
"value": "Past Due"
},
"count": 1
}]
}
Output:-
{
"notification": [{
"status": {
"code": "BILLING",
"value": "Past Due"
}
}],
"notification1": [{
"status": {
"code": "RENEWAL",
"value": "Past Due"
}
}]
}
Dataweave Code:-
payload mapObject ((value, key, index) ->
(key): value map ((item, index1) ->
item -"count"
)
)
4. Adjust DateTime Format Output
Input:-
{
"attendance": [{
"leaveTime": "2022–11–02T22:29:07Z",
"joinTime": "2022–11–02T21:00:39Z"
},
{
"leaveTime": "2022–11–02T20:59:39Z",
"joinTime": "2022–11–02T20:54:51Z"
}
]
}
Output:-
{
"attendance": [{
"leaveTime": "2022–11–02T22:29:07–04:00",
"joinTime": "2022–11–02T21:00:39–04:00"
},
{
"leaveTime": "2022–11–02T20:59:39–04:00",
"joinTime": "2022–11–02T20:54:51–04:00"
}
]
}
Dataweave Code:-
%dw 2.0
import * from dw::core::Strings
output application/json
- -
"attendance":payload.attendance map ((item, index) ->
item mapObject ((value, key, index1) ->
(key):substringBeforeLast(value, "Z") ++ "-04:00"
)
)
5. Flatten a nested JSON Array using Recursion
Input:-
[
[
[
[
[{
"a": 1
}]
],
[{
"b": 2
}]
],
[
[{
"c": 3
}]
]
],
[{
"d": 4
}]
]
Output:-
[{
"a": 1
},
{
"b": 2
},
{
"c": 3
},
{
"d": 4
}
]
Dataweave Code:-
%dw 2.0
output application/json
fun flattenRecursion(arr: Array) = do {
arr reduce ((item, acc = []) ->
item match {
case x is Array -> acc ++ flattenRecursion(x)
else -> acc << item
}
)
}
- -
flattenRecursion(payload)
6. Calculate the Total Price of each individual order
Input:-
<?xml version='1.0' encoding='UTF-8'?>
<Orders>
<Order id="10001">
<ProductId id="P001">P-001</ProductId>
<ProductName>Samsung 40Inch TV</ProductName>
<Category id="C001">Samsung TV</Category>
<Price>399</Price>
<Quantity>5</Quantity>
</Order>
<Order id="10001">
<ProductId id="P002">P-002</ProductId>
<ProductName>Samsung 32Inch TV</ProductName>
<Category id="C001">Samsung TV</Category>
<Price>299</Price>
<Quantity>5</Quantity>
</Order>
</Orders>
Output:-
[{
"totalCost": 1995
},
{
"totalCost": 1495
}
]
Dataweave Code:-
payload.Orders.*Order map(
"totalCost": $.Price * $.Quantity
)
7. Calculate the Total Price of All Orders
Input:-
Same as given on previous ques
Output:-
{
"totalCost": 3490
}
Dataweave Code:-
"totalCost" : sum(payload.Orders.*Order map(
$.Price * $.Quantity
))
8. Filter Unique Records. (Explanation:- Records should be unique globally. Pick the first unique title and discard the remaining)
Input:-
[{
"id": 1,
"books": [{
"bookid": "1234",
"title": "C#"
}, {
"bookid": "4321",
"title": "VB"
}]
},
{
"id": 2,
"books": [{
"bookid": "4321",
"title": "VB"
},
{
"bookid": "5678",
"title": "Java"
}
]
},
{
"id": 3,
"books": [{
"bookid": "1234",
"title": "C#"
},
{
"bookid": "56781",
"title": "Java"
}
]
}
]
Output:-
[{
"id": "1",
"books": [{
"bookid": "1234",
"title": "C#"
},
{
"bookid": "4321",
"title": "VB"
}
]
},
{
"id": "2",
"books": [{
"bookid": "5678",
"title": "Java"
}]
},
{
"id": "3",
"books": [{
"bookid": "56781",
"title": "Java"
}]
}
]
Dataweave Code:-
%dw 2.0
output application/json
var globalList = payload flatMap ((item) -> (
item.books map (b) -> {id: item.id,(b)}))
distinctBy ((item) -> item.bookid)
groupBy ((item) -> item.id)
- -
keysOf(globalList) map ((item) ->
{
id: item,
books: globalList[item] map ((book) -> book - 'id')
} )
9. Extract Keys from Nested Objects & Array
Input:-
[{
"key1": 1,
"key2": 2,
"key3": [{
"Key4": {
"key5": 5
}
},
{
"key6": 6
}
]
}]
Output:-
[
"key1",
"key2",
"key3",
"Key4",
"key5",
"key6"
]
Dataweave Code:-
%dw 2.0
output application/json
fun Keys(value: Any, keys = []): Array<String> | Null = do {
flatten([keys,
typeOf(value) as String match {
case "Object" -> keysOf(value) reduce (v0, a0 = flatten([keys, keysOf(value)])) -> flatten([a0, Keys(value[v0], keys)])
case "Array" -> value reduce (v1, a1 = []) -> flatten([a1, Keys(v1, keys)])
else -> []
}])
}
- -
Keys(payload)
10. Array of objects to string
Input:-
[{
"Name": "Shubham",
"language": "Java",
"Id": "101"
},
{
"Name": "Sravan",
"language": "C",
"Id": "104"
}
]
Output:-
"Name Shubham language Java Id 101 Name Sravan language C Id 104"
Dataweave Code:-
(payload flatMap ($ pluck ($$ ++ " " ++ $))) joinBy " "
📢 Check the other Articles in the Dataweave Series:- Dataweave Series for Practice & Interview Part 1–10
Thank You for sticking with the article until the end. If you found this helpful, please leave a Clap & Follow for more such amazing articles in the future.
Also Checkout More Amazing Articles:- Click Here
About Me:-
I am a MuleSoft Certified Developer & Architect working in Billennium. You can read more about me here. Follow me for Amazing Blogs & Articles.
Feel Free to Drop your queries on my below Communication Channels
Gmail:-myid535@gmail.com
LinkedIn:- https://www.linkedin.com/in/shubhamchaurasia1/