0% found this document useful (0 votes)
23 views21 pages

DW Problems

The document contains various examples of data transformation using DataWeave, a data transformation language. It demonstrates how to manipulate and reshape data structures, such as grouping, filtering, and mapping, to achieve desired outputs. Each example includes input data, the expected output, and the corresponding DataWeave solution.

Uploaded by

Vamsi Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views21 pages

DW Problems

The document contains various examples of data transformation using DataWeave, a data transformation language. It demonstrates how to manipulate and reshape data structures, such as grouping, filtering, and mapping, to achieve desired outputs. Each example includes input data, the expected output, and the corresponding DataWeave solution.

Uploaded by

Vamsi Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Input:

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

Output: {
"1": 3,
"2": 3,
"3": 3,
"4": 3,
"5": 2,
"6": 2,
"7": 2
}

Solution:
%dw 2.0
output application/json
---
((payload groupBy $)
mapObject ((value, key1, index) ->
(key1):sizeOf(value)))
orderBy ((value, key2) -> (key2))
-----------------------------------------------------------------------------------
-----------------------------------
Input:
var allocations = [
{ "allocationId": 1, "invoiceId": 1, "allocationAmount": 50 },
{ "allocationId": 2, "invoiceId": 1, "allocationAmount": 50 },
{ "allocationId": 3, "invoiceId": 2, "allocationAmount": 100 },
{ "allocationId": 4, "invoiceId": 2, "allocationAmount": 100 },
{ "allocationId": 5, "invoiceId": 3, "allocationAmount": 150 },
{ "allocationId": 6, "invoiceId": 3, "allocationAmount": 150 }
]
var invoices = [
{ "invoiceId": 1, "amount": 100 },
{ "invoiceId": 2, "amount": 200 },
{ "invoiceId": 3, "amount": 300 }
]

Output:
[
{ "invoiceId": 1,
"amount": 100,
"allocations": [
{
"allocationId": 1,
"invoiceId": 1,
"allocationAmount": 50
},
{
"allocationId": 2,
"invoiceId": 1,
"allocationAmount": 50
}
]
},
{
"invoiceId": 2,
"amount": 200,
"allocations": [
{
"allocationId": 3,
"invoiceId": 2,
"allocationAmount": 100
},
{
"allocationId": 4,
"invoiceId": 2,
"allocationAmount": 100
}
]
},
{
"invoiceId": 3,
"amount": 300,
"allocations": [
{
"allocationId": 5,
"invoiceId": 3,
"allocationAmount": 150
},
{
"allocationId": 6,
"invoiceId": 3,
"allocationAmount": 150
}
]
}
]

Solution:

invoices map{
"invoiceId": $.invoiceId,
"amount": $.amount,
"allocations": allocations filter ((item, index) -> item.invoiceId ==
$.invoiceId)
}

-----------------------------------------------------------------------------------
------------------------------
Input:
[
{ "allocationId": 1, "invoiceId": 1, "allocationAmount": 50 },
{ "allocationId": 2, "invoiceId": 1, "allocationAmount": 150 },
{ "allocationId": 3, "invoiceId": 2, "allocationAmount": 100 },
{ "allocationId": 4, "invoiceId": 2, "allocationAmount": 1000 },
{ "allocationId": 5, "invoiceId": 3, "allocationAmount": 150 },
{ "allocationId": 6, "invoiceId": 3, "allocationAmount": 150 }
]

Output:
[
{
"invoiceId": 1,
"amount": 200,
"allocations": [
{
"allocationId": 1,
"allocationAmount": 50
},
{
"allocationId": 2,
"allocationAmount": 150
}
]
},
{
"invoiceId": 2,
"amount": 1100,
"allocations": [
{
"allocationId": 3,
"allocationAmount": 100
},
{
"allocationId": 4,
"allocationAmount": 1000
}
]
},
{
"invoiceId": 3,
"amount": 300,
"allocations": [
{
"allocationId": 5,
"allocationAmount": 150
},
{
"allocationId": 6,
"allocationAmount": 150
}
]
}
]

solution-1:
%dw 2.0
output application/json
var groupList=(payload groupBy ((item, index) -> item.invoiceId))
---
keysOf(groupList) map ((item, index) -> {
"invoiceId": item as Number,
"amount": sum(groupList[item].*allocationAmount),
"allocations": groupList[item] map((gitem,gindex)->{
"allocationId": gitem.allocationId,
"allocationAmount": gitem.allocationAmount
})
})

solution-2:
%dw 2.0
output application/json
---
(payload groupBy ((item, index) -> item.invoiceId)) pluck ((value, key, index) -> {
"invoiceId": (key) as Number,
"amount": sum(value.allocationAmount),
"allocations": value map ((mitem, mindex) -> {
"allocationId": mitem.allocationId,
"allocationAmount": mitem.allocationAmount
})
})
-----------------------------------------------------------------------------------
----------------------------
Input(in CSV):
invoiceId,vendorName,total,lineItem,lineItemAmount
1,Amazon,100,Sneakers,75
1,Amazon,100,Shirt,25
2,Walmart,38,Paper,10
2,Walmart,38,Towel,28

Output:
[
{
"invoiceId": "1",
"vendorName": "Amazon",
"total": "100",
"lineItems": [
{
"lineItem": "Sneakers",
"lineItemAmount": "75"
},
{
"lineItem": "Shirt",
"lineItemAmount": "25"
}
]
},
{
"invoiceId": "2",
"vendorName": "Walmart",
"total": "38",
"lineItems": [
{
"lineItem": "Paper",
"lineItemAmount": "10"
},
{
"lineItem": "Towel",
"lineItemAmount": "28"
}
]
}
]

Solution1:
%dw 2.0
output application/json
---
(payload map ((item, index) -> {
"invoiceId":item.invoiceId,
"vendorName": item.vendorName,
"total": item.total,
"lineItems": (payload filter (item.invoiceId == $.invoiceId)) map ((item1,
index1) -> {
"lineItem": item1.lineItem,
"lineItemAmount": item1.lineItemAmount
})
})) distinctBy ((item, index) -> item.invoiceId)

Solution2:
%dw 2.0
output application/json
---
((payload groupBy ((item) -> item.invoiceId)) pluck ((value, key, index) -> value))
flatMap ((item, index) -> {
invoiceId: item[0].invoiceId,
vendorName:item[0].vendorName,
total: item[0].total,
lineItems: item map ((item1) -> {
lineItem: item1.lineItem,
lineItemAmount:item1.lineItemAmount
})
})

Input:
[
{ "merchantName": "HelloFresh" },
{ "merchantName": "Amazon" },
{ "merchantName": "Walmart" },
{ "merchantName": "Guitar Center" }
]

Output:
{
"false": [
{"merchantName": "HelloFresh"},
{"merchantName": "Guitar Center"}
],
"true": [
{"merchantName": "Amazon"},
{"merchantName": "Walmart"}
]
}

Solution:
%dw 2.0
output application/json
---
payload groupBy (sizeOf($.merchantName)< 10)

-----------------------------------------------------------------------------------
----------------------------------------
Input:
[
{"dev": 1},
{"qa": 2},
{"prod": 3}
]

output:
{
"dev": 1,
"qa": 2,
"prod": 3
}

Solution:
%dw 2.0
output application/json
---
payload reduce ($$ ++ $)

-----------------------------------------------------------------------------------
-----------------------------------
Input
[
{
"ChildAccounts": [
{
"Id": "000183837",
"type": "Account",
"Name": "Secondary Account 1"
},
{
"Id": "002683838",
"type": "Account",
"Name": "Secondary Account 2"
}
],
"Id": "27383839",
"type": "Account",
"Name": "Primary Account"
}
]

Output:
{
"data": {
"type": "Accounts",
"items": [
{
"Id": "000183837",
"Name": "Secondary Account 1"
},
{
"Id": "002683838",
"Name": "Secondary Account 2"
},
{
"Id": "27383839",
"Name": "Primary Account"
}
]
}
}

Solution-1:
%dw 2.0
output application/json
var item = payload map ((item, index) -> {
"Id": item.Id,
"Name": item.Name
})
---
{
"data":{
"type": "Accounts",
"items":(payload.ChildAccounts flatMap $) map{
"Id": $.Id,
"Name": $.Name
} ++ item
}
}

Solution-2:
%dw 2.0
output application/json
---
{
data:{
"type": "Acoounts",
items: payload map ((item) -> {
Id: item.Id,
Name: item.Name
}) ++ payload.ChildAccounts[0] map ({
Id: $.Id,
Name: $.Name
})
}
}

-----------------------------------------------------------------------------------
-------------------------------------
Input:
[
{
"name": "Raju",
"class": "1",
"marks": "55"
},
{
"name": "Rani",
"class": "2",
"marks": "60"
},
{
"name": "Phani",
"class": "2",
"marks": "66"
},
{
"name": "Rakesh",
"class": "1",
"marks": "52"
}
]

Output:
[
{
"name": "Raju",
"class": "1",
"marks": "55"
},
{
"name": "Phani",
"class": "2",
"marks": "66"
}
]

Solution-1:
%dw 2.0
output application/json
---
(payload groupBy ((item, index) -> item.class)) mapObject ((value, key, index) -> {
"records":value maxBy($.marks)
}) pluck ((value, key, index) -> value)

Solution-2:
%dw 2.0
output application/json
---
valuesOf(payload groupBy ((item) -> item.class) mapObject ((value, key) -> {
"marks": value maxBy ($.marks )
}))

-----------------------------------------------------------------------------------
--------------------------------------
payload1:
[
{
"orderId": 9,
"orderName": "order1",
"orderDate": "2024-02-12",
"orderPrice": 1000,
"orderQt": 2,
"ISOCode": "DZD"
},
{
"orderId": 10,
"orderName": "order2",
"orderDate": "2024-02-12",
"orderPrice": 3000,
"orderQt": 4,
"ISOCode": "AWG1"
}
]
payload2:
[
{
"sISOCode": "AFA",
"sName": "Afghanis"
},
{
"sISOCode": "DZD",
"sName": "Algeria Dinars"
},
{
"sISOCode": "MGA",
"sName": "Ariary"
}
]
output:
[
{
"code": "Algeria Dinars",
"order-id": 9,
"order-name": "order1",
"order-date": "2024-02-12",
"order-price": 1000,
"order-qt": 2
},
{
"code": "UNKNOWN",
"order-id": 10,
"order-name": "order2",
"order-date": "2024-02-12",
"order-price": 3000,
"order-qt": 4
}
]

Solution:
payload1 map ((item) -> {
code: (payload2 filter ($.sISOCode ~= item.ISOCode))[0].sName default
"UNKNOWN",
"order-id": item.orderId,
"order-name": item.orderName,
"order-date": item.orderDate,
"order-price": item.orderPrice,
"order-qt": item.orderQt
})

-----------------------------------------------------------------------------------
----------------------------------
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"
}
]
}
]

Solution-1:
%dw 2.0
output application/json
var a = ((payload flatMap ((item) -> item.books map ($ ++ (id:item.id))) distinctBy
((item) -> item.bookid)) groupBy ((item) -> item.id))
---
keysOf(a) map ((item) -> {
id: item,
books:a[item] map (($) - "id")
})

solution-2:
%dw 2.0
output application/json
var books = payload flatMap ((item) -> item.books map ($ ++ (id: (payload filter
((item1) -> item1.id ~= item.id)).id[0]))) distinctBy ($.bookid)
---
payload map ((item, index) -> {
id: item.id,
books: (books filter ($.id ~= item.id)) map ((item, index) -> item - "id")
})

-----------------------------------------------------------------------------------
----------------------------------------
Input:
[0,1,2,3,4,5,6,7,8,9]

Output:
{
"Even": [0,2,4,6,8],
"Odd": [1,3,5,7,9]
}

Solution:
%dw 2.0
output application/json
---
{
"Even": payload filter (($ mod 2) == 0 ),
"Odd": payload filter ((item, index) -> (item mod 2) != 0)
}

-----------------------------------------------------------------------------------
-------------------------------------
Input:
[
{
"message1":"Hello World"
},
{
"message2":"Hello Shubham"
}
]

Output:
[
{
"Hello World": "message1"
},
{
"Hello Shubham": "message2"
}
]

Solution:
%dw 2.0
output application/json
---
payload map(
$ mapObject ((value, key, index) ->{
(value): key
})
)

-----------------------------------------------------------------------------------
-----------------------------------
Input:
[
{
"ID":"A",
"name":"123",
"bossID":"C"
},
{
"ID":"B",
"name":"345",
"bossID":"D"
},
{
"ID":"C",
"name":"456",
"bossID":"B"
},
{
"ID":"D",
"name":"789",
"bossID":"C"
}
]

Output:
[
{
"EmpName": "123",
"BossName": "456"
},
{
"EmpName": "345",
"BossName": "789"
},
{
"EmpName": "456",
"BossName": "345"
},
{
"EmpName": "789",
"BossName": "456"
}
]

Solution:
%dw 2.0
output application/json
---
payload map ((item, index) -> {
"EmpName": item.name,
"BossName": (payload filter($.ID == item.bossID)).name[0]
})

-----------------------------------------------------------------------------------
-------------------------------------
Input:
[
{
"name": "abc",
"city": "hyd"
},
{
"name": "abc",
"city": "delhi"
},
{
"name": "abc1",
"city": "hyd"
}
]
Output:
"abc,abc1"

Solution:
%dw 2.0
output application/json
---
((payload filter ((item, index) -> item.city == "hyd"))map ($.name)) joinBy ","

-----------------------------------------------------------------------------------
-----------------------------------
Input:
{
"a": ["12","15","17"],
"b":["15","18","20","12","89"]
}

Output:
[
"12",
"15"
]

Solution1:
%dw 2.0
output application/json
---
payload.a map(payload.b filter((item, index) -> item == $)) flatMap ($)
solution2:
%dw 2.0
output application/json
---
payload.a filter ((item, index) -> payload.b contains item)

-----------------------------------------------------------------------------------
------------------------------------
Input:
{
"arrivalDateTime": "",
"cabin": {},
"fareComponent": [],
"flightNumber": "3234"
}

Output:
{
"flightNumber": "3234"
}

Solution:
%dw 2.0
output application/json
---
payload filterObject ((value, key, index) -> sizeOf(value) != 0)

-----------------------------------------------------------------------------------
-------------------------------------
Input:
{
"Name":"Pallav",
"Marks":96,
"Name":"Atharv",
"Marks":94,
"Name":"Rajnish",
"Marks":92
}

Output:
[
{
"Name":"Pallav",
"Marks": 96,
},
{
"Name":"Atharv",
"Marks": 94,
},
{
"Name":"Rajnish",
"Marks": 92,
}
]
Solution:
%dw 2.0
import * from dw::core::Objects
output application/json
---
payload divideBy 2

-----------------------------------------------------------------------------------
------------------------------------
Input:
[
{
"firstName": "John",
"lastName": "Doe",
"jobTitle": "Engineer",
"department": "Technology"
},
{
"firstName": "Smith",
"lastName": "Doe",
"jobTitle": "Engineer",
"department": "Technology"
},
{
"firstName": "Kelly",
"lastName": "Doe",
"jobTitle": "Analyst",
"department": "Operations"
},
{
"firstName": "Keith",
"lastName": "Doe",
"jobTitle": "Analyst",
"department": "Finance"
}
]

Output:
[
{
"department": "Technology",
"count": 2
},
{
"department": "Operations",
"count": 1
},
{
"department": "Finance",
"count": 1
}
]

Solution:
%dw 2.0
output application/json
import * from dw::core::Arrays
var depatments = payload map ((item, index) -> {
"department": item.department
})
---
payload map ((item, index) -> {
"department": item.department,
"count": depatments countBy (($.department ~= item.department))
})distinctBy ((item, index) -> item)

-----------------------------------------------------------------------------------
------------------------------------
Input:
["a","b","c","a","b","a"]

Output:
[
"a3",
"b2",
"c1"
]

Solution-1:
%dw 2.0
output application/json
import * from dw::core::Arrays
---
payload map ((item, index) -> item ++ (payload countBy($ == item))) distinctBy
((item, index) -> item)

Solution-2:
%dw 2.0
output application/json
var a = ((payload groupBy ($)) mapObject ({"$$": sizeOf($)}))
---
(payload distinctBy ((item) -> item)) map ((item) -> item ++ (valuesOf(a
filterObject ($$ ~= item))reduce ($$)))

Solution-3:
%dw 2.0
output application/json
var size = payload groupBy ($) mapObject (($$): sizeOf($))
---
valuesOf(size mapObject ((value, key) -> (key): key ++ value))

-----------------------------------------------------------------------------------
---------------------------------------
Input:
[{
"key1": "address",
"value": "add-1"
},
{
"key1": "address",
"value": "add-2"
},
{
"key1": "postal",
"value": "208021"
}
]

Output:
[
{
"address": "add-1 add-2"
},
{
"postal": "208021"
}
]

Solution:
%dw 2.0
output application/json
---
payload groupBy ($.key1) pluck (($$): $.value joinBy " ")

-----------------------------------------------------------------------------------
---------------------------------
Input:
[
{
"name": "Raju",
"class": "1",
"marks": "55"
},
{
"name": "Rani",
"class": "2",
"marks": "60"
},
{
"name": "Phani",
"class": "2",
"marks": "66"
},
{
"name": "Rakesh",
"class": "1",
"marks": "52"
}
]

output:
[
{
"name": "Raju",
"class": "1",
"marks": "55"
},
{
"name": "Phani",
"class": "2",
"marks": "66"
}
]
solution-1:
payload maxBy ((item) -> item.marks)

solution-2:
payload groupBy (item) -> item.class mapObject ((value, key) -> {
class: key,
maxMarks: max(value.marks as Number)
})

-----------------------------------------------------------------------------------
----------------------------------------
Input: Print the most repetitive character in a string
"DataWeave is awesome!"

Output:
[
"a",
"e"
]

Solution:
%dw 2.0
output application/json
var a = (payload replace " " with "" splitBy "" groupBy ($) mapObject ((value,
key, index) -> (key): sizeOf(value)))
---
keysOf(a filterObject ((value, key) -> value == (valuesOf(a) maxBy ((item) ->
item))))

-----------------------------------------------------------------------------------
-----------------------------------------
Input:
<?xml version='1.0' encoding='UTF-8'?>
<ROOT>
<acc acctype="DEP">D1111</acc>
<acc acctype="LN">12345</acc>
<acc acctype="DEP">D2222</acc>
</ROOT>

Output:
{
"DEP": "D1111",
"DEP": "D2222"
}

Solution:
%dw 2.0
output application/json
---
payload.ROOT mapObject ((value, key) -> (key.@acctype): value) filterObject
((value, key) -> key ~= "DEP")

-----------------------------------------------------------------------------------
--------------------------------------
Input:
[
{
"Name": "rajnish_kant"
},
{
"Name": "anirudh_bhardwaj"
}
]

Output:
"RajnishKant,AnirudhBhardwaj"
["RajnishKant","AnirudhBhardwaj"]

Solution:
%dw 2.0
output application/json
import * from dw::core::Strings
---
(payload map (capitalize(($.Name)) replace " " with "")) joinBy ","

-----------------------------------------------------------------------------------
------------------------------------
Input:
{
"Employee": [
{
"Code": " 501-AKASH",
"Age": "26",
"Salary":[{
"Base": " 330000 "
}],
"Date":{
"Joining":" 26/10/2020 "
},
"Billed": [" No "]
},
{
"Code": " 502-Priya",
"Age": "30 ",
"Salary":[{
"Base": "220000 "
}],
"Date":{
"Joining":" 26/10/2020 "
},
"Billed": [" Yes "]
}
]
}

Output:
{
"Employee": [
{
"Code": "501-AKASH",
"Age": "26",
"Salary": [
{
"Base": "330000"
}
],
"Date": {
"Joining": "26/10/2020"
},
"Billed": [
"No"
]
},
{
"Code": "502-Priya",
"Age": "30",
"Salary": [
{
"Base": "220000"
}
],
"Date": {
"Joining": "26/10/2020"
},
"Billed": [
"Yes"
]
}
]
}

Solution-1:
%dw 2.0
output application/json
fun trimVal (value) = value match {
case is String -> trim($)
case is Array -> value map (trimVal($))
case is Object -> value mapObject {($$): trimVal($)}
else -> $
}

---
trimVal(payload)

Solution-2:
%dw 2.0
output application/json
import * from dw::core::Strings
---
payload mapObject{
($$): $ map ((item, index) -> {
"Code": trim(item.Code),
"Age": trim(item.Age),
"Salary": item.Salary map ((item1) -> item1 mapObject ((value, key, index)
-> {(key):trim(value)})),
"Date": item.Date mapObject ((value, key) -> {
(key): trim(value)
}),
"Billed": item.Billed map ((item2) -> trim(item2))
})
}
-----------------------------------------------------------------------------------
--------------------------------------
Input:
{
"NameList": "Shubh,Priya"
}

Output:
[
{
"Student 0": "Shubh"
},
{
"Student 1": "Priya"
}
]

Solution:
%dw 2.0
output application/json
---
((payload.NameList) splitBy ",") map {
("Student " ++ ($$)): $
}

-----------------------------------------------------------------------------------
------------------------------------
Input:
{
"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
}

Solution:
%dw 2.5
output application/json
fun flattenObj(obj) = obj mapObject ( if($ is Object) flattenObj($) else ($$):$)
---
flattenObj(payload)

You might also like