DataWeave 25+ Solved Examples


         

1

How to avoid null values/ empty value inBulk data.

Input

DW Lang

Output

[

   {

      "name":"Me",

      "address":"add-1",

      "zip":"1234"

   },

   {

      "name":"Me2",

      "address":"add-2",

      "zip":""

   }

]

%dw 2.0

output application/json

---

payload map ((item, index) -> {

    "name": item.name,

    "address": item.address,

    "zip": item.zip

}) filter (!isEmpty($.zip))

--------OR--------------

%dw 2.0

output application/json

---

payload filter (!isEmpty($.zip))

 

[

   {

      "name":"Me",

      "address":"add-1",

      "zip":"1234"

   }

]

 

2

Calculate All semester marks into CGP

Input

DW Lang

Output

[

  {

    "sid": 101,

    "sname": "Rahul",

    "sem": 1,

    "marks": 9

  },

  {

    "sid": 101,

    "sname": "Rahul",

    "sem": 2,

    "marks": 9.5

  },

  {

    "sid": 101,

    "sname": "Rahul",

    "sem": 3,

    "marks": 8

  },

  {

    "sid": 101,

    "sname": "Rahul",

    "sem": 4,

    "marks": 7

  }

]

%dw 2.0

var abc = payload distinctBy $.sid

output application/json 

---

abc map ((item, index) -> {

  "SID": item.sid,

  "SNAME": item.sname,

  "CGP": payload filter ($.sid == item.sid) reduce (item, avg = 0) -> avg + item.marks / sizeOf(payload filter ($.sid == item.sid))

})

 

[

  {

    "SID": 101,

    "SNAME": "Rahul",

    "CGP": 8.375

  }

]

 

 

3

How to remove vowels from string.

Input

DW Lang

Output

 

 

"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

%dw 2.0

output application/json

var a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

---

a filter ($ != 'A' and $ != 'E' and $ != 'I' and $ != 'O' and $ != 'U')

 

or

 

%dw 2.0

output application/json

var a = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

var vowel = ['A','E','I','O','U']

---

a filter !(vowel contains $)

 

 

 

"BCDFGHJKLMNPQRSTVWXYZ"

 

4

How to change phone number format.

Input

DW Lang

Output

{

    "phone": 9876543210

}

%dw 2.0

 

fun toPhoneFormat(str: String) =

  str[0 to 2] ++ "-" ++ str[3 to 5] ++ "-" ++ str[6 to 9]

output application/json 

---

 

"phone": toPhoneFormat(payload.phone)

 

 

 

{

  "phone": "987-654-3210"

}

 

 

5

Remove Special character in string.

Input

DW Lang

Output

{

    "message":"@300"

}

 

%dw 2.0

output application/json

---

payload.message splitBy("@") joinBy  ""

(or)

payload.message replace  "@" with ""

 

 

300

 

6

Find Even or Odd from given number

Input

DW Lang

Output

{

    "number":"309"

}

 

 

%dw 2.0

output application/json

var result = payload.number mod 2

---

 

   if (result == 0)

      { number: "Even" }

   else {  number: "Odd" }

 

 

 

{

  "number": "Odd"

}

 

 

7

How to change keys and vales vice versa object

Input

DW Lang

Output

[

  {

    "SourceFieldName": "CUSTOMER_NAME",

    "DestinationFieldName": "Name"

  },

  {

    "SourceFieldName": "CUSTOMER_ADDRESS",

    "DestinationFieldName": "Address"

  }

]

 

 

%dw 2.0

output application/json

---

payload map ((item, index) -> {

    (item.SourceFieldName): item.DestinationFieldName

}) reduce ((item, accumulator) ->item++ accumulator )

 

 

 

{

  "CUSTOMER_ADDRESS": "Address",

  "CUSTOMER_NAME": "Name"

}

 

 

8

How to get total Salary for employee Mahesh

Input

DW Lang

Output

{
"employees": [
{
"empname": "kishore",
"empid": "1234",
"empsal": "50000"
},
{
"empname": "mahesh",
"empid": "1235",
"empsal": "60000"
},
{
"empname": "kavya",
"empid": "12345",
"empsal": "70000"
},
{
"empname": "mahesh",
"empid": "12335",
"empsal": "70000"
},
{
"empname": "mahesh",
"empid": "1235efr",
"empsal": "70000"
},
{
"empname": "mahesh",
"empid": "1235232",
"empsal": "50000"
}
]
}

%dw 2.0

output application/json

var a = payload.employees filter $.empname == "mahesh"

---

payload.employees filter $.empname == "mahesh" map ((item, index) -> {

    "empsal": sum(a.empsal)

})distinctBy $

)

 

 

 

[

  {

    "empsal": 250000

  }

]

 

 

9

How to remove $ from string.

Input

DW Lang

Output

 

{

    "number": "$500"

}

 

%dw 2.0

output application/json

---

payload.number splitBy  "\$"  joinBy ""

or

payload.number replace  /([\$])/ with ""

or

payload.number[1 to 3]

 

"500"

 

 

 

10

How to get “Uat” from below input?

Input

DW Lang

Output

["prod",["test", "Uat", "dev"]]

 

 

%dw 2.0

output application/json

---

payload[1][1]

 

 or

 

flatten(payload)[2]

 

"Uat"

 

 

11

Sort a string by alphabetically

Input

DW Lang

Output

{

    "name""a,c,d,e,b"

}

 

 

%dw 2.0

output application/json

---

payload.name splitBy  "," orderBy $ joinBy ","

 

"a,b,c,d,e"

 

 

12

How to make 3rd letter in string is upper

Input

DW Lang

Output

{

    "name": "hi how are you"

}

 

 

%dw 2.0

import * from dw::util::Values

var split = (payload.name splitBy "")

output application/json 

---

(split update 3 with upper(split[3])) joinBy ("")

 

"hi How are you"

 

 


14

Date format

Input

DW Lang

Output

{

    " IDate": "18032022"

}

 

 

 

%dw 2.0

output application/json 

---

payload.IDate as Date {format: "ddMMyyyy"} as Date {format: "dd/MM/yyyy"}

 

"18/03/2022"

 

 

13

How to combine letters to word

Input

DW Lang

Output

[ "H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", "!" ]

 

%dw 2.0

output application/json 

---

payload joinBy  ""

 

or

 

payload reduce ((item, accumulator) ->accumulator ++ item )

 

"Hello world!"

 


15

How to check whether a string is Palindrome or not

Input

DW Lang

Output

{

    "message": "mom"

}

 

%dw 2.0

import * from dw::core::Strings

var a = payload.message

output application/json 

---

{

  "message":

    if (reverse(payload.message) == payload.message)

      " palindrome"

 else"not palindrome"

}

{

  "message": "palindrome"

}

 

 

16

How to get count of  “abc” soap.

Input

DW Lang

Output

[

   {

      "soap":"rin",

      "amount":"20"

   },

   {

      "soap":"vim",

      "amount":"50"

   },

   {

      "soap":"abc",

      "amount":"80"

   },

    {

      "soap":"abc",

      "amount":"90"

   },

    {

      "soap":"abc",

      "amount":"60"

   },

]

 

%dw 2.0

output application/json

---

sizeOf(payload filter $.soap == "abc" )

 

 

3

 

17

How to remove key and value for data3.

Input

DW Lang

Output

{

  "attributes": {

    "data1": [

      {

        "field1": "some data"

      }

    ],

    "data2": [

      {

        "field2": "some data"

      }

    ],

    "data3": [

      {

        "field": "some data",

        "name": "mani"

      }

    ]

  }

}

%dw 2.0

output application/json

---

payload.attributes -"data3"

 

{

  "data1": [

    {

      "field1": "some data"

    }

  ],

  "data2": [

    {

      "field2": "some data"

    }

  ]

}

 

 

18

How to remove “name” key and value in Object

Input

DW Lang

Output

[

   {

      "id":1001,

      "name":"mani"

   },

   {

      "id":1002,

      "name":"Rojendar"

   },

   {

      "id":1003,

      "name":"Velpula"

   }

]

 

%dw 2.0

output application/json

---

payload map $ - "name"

 

[

  {

    "id": 1001

  },

  {

    "id": 1002

  },

  {

    "id": 1003

  }

]

 

 

19

Object to Array

Input

DW Lang

Output

{

  "one": "two",

  "three": "four",

  "five": "six"

}

%dw 2.0

output application/json

---

payload pluck ((value, key, index) ->key++","++ value ) map ($ splitBy ",")

 

[

  [

    "one", "two"

  ],

  [

    "three", "four"

  ],

  [

    "five", "six"

  ]

]

 

 

20

How to Swap key and values

Input

DW Lang

Output

[

    {

        "name": "mani",

        "sn": 11

    },

    {

        "name": "anil",

        "sn": 12

    }

]

%dw 2.0

import * from dw::core::Strings

output application/json

---

payload map ((item, index) -> {

   (item.name): "name",   (item.sn): "sno" })

 

-----or---

 

payload map ((item, index) -> item mapObject ((value, key, index) -> {

    (key): value

}))

 

[

  {

    "mani": "mani",

    "sn": 11

  },

  {

    "name": "anil",

    "sn": 12

  }

]

 

 

21

How to remove characters in given string

Input

DW Lang

Output

{

    "name": "12mani455how"

}

 

%dw 2.0

import * from dw::core::Strings

output application/json 

---

(payload.name splitBy  "" filter ((item, index) -> isNumeric(item)) joinBy  "" ) as String

 

"12455"

 


 

22

Sort the data from XML to JSON and ascending order

Input

DW Lang

Output

<?xml version='1.0' encoding='UTF-8'?>

<employess>

  <employe>

    <id>1</id>

    <name>Max</name>

    <dept>Hr</dept>

  </employe>

  <employe>

    <id>3</id>

    <name>Mule</name>

    <dept>Hr</dept>

  </employe>

    <employe>

    <id>2</id>

    <name>Alex</name>

    <dept>Admin</dept>

  </employe>

</employess>

 

%dw 2.0

output application/json 

---

"employess": payload.employess.*employe map ((item, index) -> {

    "id": item.id,

    "name": item.name

}) orderBy $.id

 

 

{

  "employess": [

    {

      "id": "1",

      "name": "Max"

    },

    {

      "id": "2",

      "name": "Alex"

    },

    {

      "id": "3",

      "name": "Mule"

    }

  ]

}

 


 

23

How to split path as filename and file path

Input

DW Lang

Output

 

{

    "fileSharePath": "D:\\Muleworkspace\\choice\\mani.txt"

}

 

 

%dw 2.0

output application/json 

var v = payload.fileSharePath splitBy  "\\"

---

payload mapObject ((value, key, index) ->{

    "fileName": v[-1],

    "filePath": value

} )

 

 

{

  "fileName": "mani.txt",

  "filePath": "D:\\Muleworkspace\\choice\\mani.txt"

}

 

 

24

Convert XML to JSON

Input

DW Lang

Output

 

<?xml version="1.0" encoding="UTF-8"?>

<users>

   <user>

      <name>Mani Mulesoft</name>

      <phone>5534567889990087655</phone>

      <street>Laprida 924</street>

   </user>

   <user>

      <name>Anil Alejandro Cousido</name>

      <phone>9945678899900876544</phone>

      <street>Acassuso 2280</street>

   </user>

</users>

 

%dw 2.0

output application/json 

---

"contact": payload.users.*user map ((item, index) ->

  {

      "firstName": (item.name splitBy " ")[0],

      "lastname": (item.name splitBy " ")[1],

      "email": lower((item.name) replace  " " with "." ++  "@"++ "mani.com"),

      "address": item.street

    } )

 

 

 

{

  "contact": [

    {

      "firstName": "Mani",

      "lastname": "Mulesoft",

      "email": "mani.mulesoft@mani.com",

      "address": "Laprida 924"

    },

    {

      "firstName": "Anil",

      "lastname": "Alejandro",

      "email": "anil.alejandro.cousido@mani.com",

      "address": "Acassuso 2280"

    }

  ]

}

 

 

25

How to find top 3  student data

Input

DW Lang

Output

{

 "availablePositions": 3,

 "candidates":

    [{

        "name":"Gunther Govan",

        "score":99

      }

      {

        "name":"Amalia Silva",

        "score":96

      },

       {

        "name":"Tom Mathews",

        "score":40

      },

      {

        "name":"Simon Wilson",

        "score":84

      },

      {

        "name":"Janet Nguyen",

        "score":52

      }

    ]

}

 

%dw 2.0

output application/json

---

{

    TopCandidateList: (payload.candidates map ((candidate) -> {

        firstName: candidate.name,

        rank: candidate.score

    }) orderBy $.rank) [ -payload.availablePositions to -1]

}

 

 

 

{

  "TopCandidateList": [

    {

      "firstName": "Simon Wilson",

      "rank": 84

    },

    {

      "firstName": "Amalia Silva",

      "rank": 96

    },

    {

      "firstName": "Gunther Govan",

      "rank": 99

    }

  ]

}

 

 

26

Combined two variables data

Input

DW Lang

Output

var a =[

   {

      "id":"3",

      "name":"C"

   },

   {

      "id":"2",

      "name":"B"

   },

   {

      "id":"4",

      "name":"D"

   }

]

 

var payload2=[

 {

  "id": "1",

  "name": "A"

 },

 {

  "id": "2",

  "name": "B",

  "Country" : "India"

 }

]

 

%dw 2.0

output application/json skipNullOn = "everywhere"

 

---

(a ++  payload2)  map ($ ++ {

       "country" : payload2[$$].Country

}) distinctBy $.id orderBy $.id

 

 

 

[

  {

    "id": "1",

    "name": "A"

  },

  {

    "id": "2",

    "name": "B",

    "country": "India"

  },

  {

    "id": "3",

    "name": "C"

  },

  {

    "id": "4",

    "name": "D"

  }

]

 

 

 Thanks & Regards 

Mani Rojendar Velpula

mani.mulesoft@gmail.com

linkedIn: https://www.linkedin.com/in/manirojendarvelpula/

Comments

  1. Thank you for sharing ur knowledge, it is helpful every one

    ReplyDelete
  2. thanks for Sharing the above content, IT would really helpful concept on dataweave and develop logic.

    ReplyDelete
  3. Fantastic content, Mani. It was beneficial. Many Thanks to you

    ReplyDelete
  4. Really helpful for beginners who is starting dataweave in Mulesoft. Thanks Mani

    ReplyDelete
  5. Very creative questions and solutions, really helped me in practicing dataweave, Really appreciate your efforts

    ReplyDelete

Post a Comment