Skip to content

anky123/dataweave-scripts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 

Repository files navigation

DataWeave Scripts

1. filter

  • Extract an object from an array of object based on employee age.

Input
{
  "status": "success",
  "data": [
  	{
  		"id": "1",
  		"employee_name": "Tiger Nixon",
  		"employee_salary": 320800,
  		"employee_age": 61,
  		"profile_image": ""
  	},
  	{
  		"id": "2",
  		"employee_name": "Garrett Winters",
  		"employee_salary": 170750,
  		"employee_age": 63,
  		"profile_image": ""
  	},
  	{
  		"id": "3",
  		"employee_name": "Ashton Cox",
  		"employee_salary": 86000,
  		"employee_age": 58,
  		"profile_image": ""
  	},
  	{
  		"id": "4",
  		"employee_name": "Cedric Kelly",
  		"employee_salary": 433060,
  		"employee_age": 22,
  		"profile_image": ""
  	}
  ]
}
Output
[
{
  "id": "4",
  "employee_name": "Cedric Kelly",
  "employee_salary": 433060,
  "employee_age": 22,
  "profile_image": ""
}
]
Transform
%dw 2.0
output application/json
---
payload.data filter ((item, index) -> item.employee_age <= 22)

2. map

  • Replace keys of an array with its index position.

Input
[
  {
  	"name": "Jane"
  },
  {
  	"name": "John"
  },
  {
  	"name": "Joe"
  }
]
Output
[
  {
  	"user 1": "Jane"
  },
  {
  	"user 2": "John"
  },
  {
  	"user 3": "Joe"
  }
]
Transform
%dw 2.0
output application/json
---
payload map ((item, index) -> {
  ("user " ++ (index +1)) : item.name
})

3. zip

  • Merge two arrays in sync with each index position.

Input 1
[
  "12345",
  "67890"
]
Input 2
[
  "abc",
  "xyz"
]
Output
[
  [
  	"12345",
  	"abc"
  ],
  [
  	"67890",
  	"xyz"
  ]
]
Transform
%dw 2.0
output application/json
import leftJoin from dw::core::Arrays
var otherInput = [
  "abc",
  "xyz"
]
---
payload zip otherInput

4. distinctBy

  • Merge two arrays based on distinct values.

Input
{
  "array1": [
  	1,
  	2,
  	3,
  	5,
  	6,
  	7
  ],
  "array2": [
  	5,
  	7,
  	1,
  	3,
  	8,
  	4
  ]
}
Output
[
  8,
  7,
  6,
  5,
  4,
  3,
  2,
  1
]
Transform
%dw 2.0
output application/json
---
((payload.array1 ++ payload.array2) distinctBy $)  orderBy -$

5. substringAfter

  • Transform a json to xml in pre-defined format.

Input
[
  {
  	"fullname": "Grace Kelly",
  	"age": "22",
  	"hiredate": "October 15, 2005",
  	"product": "Printer"
  },
  {
  	"fullname": "Cary Grant",
  	"age": "32",
  	"hiredate": "October 20, 2005",
  	"product": "Desktop"
  },
  {
  	"fullname": "Clark Gable",
  	"age": "42",
  	"hiredate": "October 25, 2005",
  	"product": "Keyboard"
  }
]
Output
<?xml version='1.0' encoding='UTF-8'?>
<employees>
  <employee>
  	<name age="22">
  		<lastname>Kelly</lastname>
  		<firstname>Grace</firstname>
  	</name>
  	<hiredate>October 15, 2005</hiredate>
  	<projects>
  		<project>
  			<product>Printer</product>
  		</project>
  	</projects>
  </employee>
  <employee>
  	<name age="32">
  		<lastname>Grant</lastname>
  		<firstname>Cary</firstname>
  	</name>
  	<hiredate>October 20, 2005</hiredate>
  	<projects>
  		<project>
  			<product>Desktop</product>
  		</project>
  	</projects>
  </employee>
  <employee>
  	<name age="42">
  		<lastname>Gable</lastname>
  		<firstname>Clark</firstname>
  	</name>
  	<hiredate>October 25, 2005</hiredate>
  	<projects>
  		<project>
  			<product>Keyboard</product>
  		</project>
  	</projects>
  </employee>
</employees>
Transform
%dw 2.0
output application/xml
import * from dw::core::Strings
---
employees : {
  employee: payload map ((item, index) -> {
      name: {
          lastname : substringAfter(item.fullname, " "), 
          firstname : substringBefore(item.fullname, " ")
      },
      hiredate : item.hiredate,
      projects : project : product : item.product
  })
}

6. reduce

  • Get only the values from two arrays in a ascending order.

Input
{
  "data1": [
  	{
  		"p_name": "3",
  		"p_val": "4"
  	},
  	{
  		"p_name": "7",
  		"p_val": "8"
  	},
  	{
  		"p_name": "1",
  		"p_val": "2"
  	}
  ],
  "data2": [
  	{
  		"p_name": "1",
  		"p_val": "2"
  	},
  	{
  		"p_name": "3",
  		"p_val": "4"
  	},
  	{
  		"p_name": "5",
  		"p_val": "6"
  	}
  ]
}
Output
{
  "1": "2",
  "3": "4",
  "5": "6",
  "7": "8"
}
Transform
%dw 2.0
output application/json
---
(((payload.data1 ++ payload.data2) map ((item, index) -> (item.p_name): item.p_val) distinctBy $) reduce ((item, accumulator) -> item ++ accumulator)) orderBy $

7. groupBy

  • Group peoples from array object based on dept.

Input
[
  {
  	"id": "123",
  	"name": "rakesh",
  	"dept": "cse"
  },
  {
  	"id": "234",
  	"name": "kumar",
  	"dept": "cse"
  },
  {
  	"id": "456",
  	"name": "suraj",
  	"dept": "IT"
  },
  {
  	"id": "489",
  	"name": "prakash",
  	"dept": "IT"
  }
]
Output
[
  {
  	"cse": [
  		"rakesh",
  		"kumar"
  	]
  },
  {
  	"IT": [
  		"suraj",
  		"prakash"
  	]
  }
]
Transform
%dw 2.0
output application/json
---
payload groupBy ((item, index) -> item.dept) pluck ((value, key, index) -> (key):value.name)

8. contains

  • Merge two json arrays based on matching id.

Input
[
  {
  	"name": "Abc",
  	"address": "Mumbai",
  	"phone": "91-1234567891",
  	"id": 123
  },
  {
  	"name": "Pqr",
  	"address": "Pune",
  	"phone": "91-1234985438",
  	"id": 456
  },
  {
  	"name": "Abc",
  	"address": "Delhi",
  	"phone": "91-1234567891",
  	"id": 341
  }
]
Output
[
  {
  	"name": "Abc",
  	"address": "Mumbai",
  	"phone": "91-1234567891",
  	"id": 123,
  	"sid": 123,
  	"postalCode": "67890",
  	"country": "USA"
  },
  {
  	"name": "Pqr",
  	"address": "Pune",
  	"phone": "91-1234985438",
  	"id": 456,
  	"sid": 456,
  	"postalCode": "54983",
  	"country": "UK"
  },
  {
  	"name": "Abc",
  	"address": "Delhi",
  	"phone": "91-1234567891",
  	"id": 341,
  	"sid": 341,
  	"postalCode": "27932",
  	"country": "EU"
  }
]
Transform
%dw 2.0
var otherArray = [
{
  "sid": 123,
  "postalCode": "67890",
  "country": "USA"
},
{
  "sid": 456,
  "postalCode": "54983",
  "country": "UK"
},
{
  "sid": 341,
  "postalCode": "27932",
  "country": "EU"
}
]
output application/json  
---
payload map (array1Value) -> {
(otherArray filter ($.sid contains array1Value.id) map (array2Value) -> {
  name: array1Value.name,
  address: array1Value.address,
  phone: array1Value.phone,
  id: array1Value.id,
  sid: array2Value.sid,
  postalCode: array2Value.postalCode,
  country: array2Value.country
})
}

9. map

  • Convert xml to a json.
  • Append first and last name into full name.
  • Calculate totalAmount from list of pricesss.

Input
<order>
  <orderId>12345</orderId>
  <customer>
  	<firstName>John</firstName>
  	<lastName>Doe</lastName>
  </customer>
  <items>
  	<item>
  		<productName>Laptop</productName>
  		<quantity>2</quantity>
  		<price>1200.00</price>
  	</item>
  	<item>
  		<productName>Printer</productName>
  		<quantity>1</quantity>
  		<price>300.00</price>
  	</item>
  </items>
</order>
Output
{
"orderId": "12345",
"customerName": "John Doe",
"totalAmount": 1500,
"items": [
  {
    "productName": "Laptop",
    "quantity": "2"
  },
  {
    "productName": "Printer",
    "quantity": "1"
  }
]
}
Transform
%dw 2.0
output application/json  
---
{
orderId: payload.order.orderId,
customerName: payload.order.customer.firstName ++ " " ++ payload.order.customer.lastName,
totalAmount: sum(payload.order.items.*item.price map ((item, index) -> item as Number)),
items: payload.order.items.*item map ((item, index) -> {
  productName: item.productName,
  quantity: item.quantity
})
}

10. pluck

  • Convert xml to a json
  • Create list of array from keys
  • Prepare list of values of each key

Input
<notes>
  <note>
  	<to>Srikant</to>
  	<from>Candidate</from>
  	<heading>Questions</heading>
  	<body>Answer that! wowo</body>
  </note>
  <note>
  	<to>Ankit</to>
  	<from>Candidate2</from>
  	<heading>Questions</heading>
  	<body>Answer that! wowo</body>
  </note>
  <note>
  	<to>Aditya</to>
  	<bcc>Sunita</bcc>
  	<from>Candidate3</from>
  	<heading>Questions</heading>
  	<body>Answer that! wowo</body>
  </note>
</notes>
Output
[
  {
  	"to": [
  		"Srikant",
  		"Ankit",
  		"Aditya"
  	]
  },
  {
  	"from": [
  		"Candidate",
  		"Candidate2",
  		"Candidate3"
  	]
  },
  {
  	"heading": [
  		"Questions",
  		"Questions",
  		"Questions"
  	]
  },
  {
  	"body": [
  		"Answer that! wowo",
  		"Answer that! wowo",
  		"Answer that! wowo"
  	]
  },
  {
  	"bcc": [
  		"Sunita"
  	]
  }
]
Transform
%dw 2.0
output application/json
---
payload.notes mapObject ((value) -> value ) groupBy ((value, key) -> key) pluck ((value, key, index) -> (key) : valuesOf(value))

11. pluck

  • Group the Fruit list.
  • Prepare the total count of fruits.

Input
[
  {
  	"Fruit": "apple",
  	"quantity": "14"
  },
  {
  	"Fruit": "orange",
  	"quantity": "13"
  },
  {
  	"Fruit": "apple",
  	"quantity": "6"
  },
  {
  	"Fruit": "orange",
  	"quantity": "12"
  }
]
Output
[
  {
  	"Fruit": "apple",
  	"quantity": 20
  },
  {
  	"Fruit": "orange",
  	"quantity": 25
  }
]
Transform
%dw 2.0
output application/json  
---
payload groupBy ((item, index) -> item.Fruit) pluck ((value, key, index) -> {
"Fruit": (key),
"quantity": sum(value.quantity)
})

12. reduce

  • Merge list of two array into one.
  • Add Mr. or Ms. based on geneder values (M or F).

Input
[
  [
  	{
  		"name": "john",
  		"gender": "M"
  	},
  	{
  		"name": "leonardo",
  		"gender": "M"
  	}
  ],
  [
  	{
  		"name": "leonardo",
  		"gender": "M"
  	},
  	{
  		"name": "alicia",
  		"gender": "F"
  	},
  	{
  		"name": "jennifer",
  		"gender": "F"
  	},
  	{
  		"name": "john",
  		"gender": "M"
  	}
  ]
]
Output
[
  "Mr.john",
  "Mr.leonardo",
  "Ms.alicia",
  "Ms.jennifer"
]
Transform
%dw 2.0
output application/json  
---
((payload map ((item, index) -> item) reduce ((val, acc) -> acc ++ val)) map ((item1, index1) -> {
"name": (if (item1.gender == "F")
  "Ms."
else
  "Mr.") ++ item1.name
}) distinctBy $) map ((item, index) -> item.name)

13. distinctBy

  • Filter the objects based on distinct id

Input
[
  {
  	"name": "Ankur",
  	"id": 1,
  	"skill": "Mule"
  },
  {
  	"name": "Ankur",
  	"id": 2,
  	"skill": "Mulesoft"
  },
  {
  	"name": "Ankur",
  	"id": 1,
  	"skill": "Mule"
  }
]
Output
[
  {
  	"name": "Ankur",
  	"id": 1,
  	"skill": "Mule"
  },
  {
  	"name": "Ankur",
  	"id": 2,
  	"skill": "Mulesoft"
  }
]
Transform
%dw 2.0
output application/json  
---
payload distinctBy $.id

About

Respository for dataweave code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published