Dataweave Series for Practice & Interview Part-1

Shubham Chaurasia
2 min readJun 20, 2022

--

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.

  1. Filter Odd & Even Numbers from an Array

Input:-

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

Output:-

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

Dataweave Code:-

{
"Even": payload filter (($ mod 2)==0),
"Odd": payload filter !(($ mod 2)==0)
}

2. Interchange Key- Value in an object

Input:-

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

Output:-

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

Dataweave Code:-

payload map ((item, index) -> 
item mapObject(v,k,n)-> {
(v) : k
}
)

3. Reverse the Words of a Sentence

Input:-

{
"Reverse": "Dataweave 2.0 is Cool"
}

Output:-

"Cool is 2.0 Dataweave"

Dataweave Code:-

(payload.Reverse splitBy(' '))[-1 to 0] joinBy(' ')

4. Filter an Array by an Objects field Value(e.g., Year)

Input:-

[
{
"Product":"A",
"Year":"2020"
},
{
"Product":"B",
"Year":"2019"
}
]

Output:-

[
{
"Product": "A",
"Year": "2020"
}
]

Dataweave Code:-

payload filter $.Year>2019

5. Filter Even Values out from any array

Input:-

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

Output:-

[0,2,4,6,8]

Dataweave Code:-

payload filter isEven($)

6. Print Last x characters of a number or String

Input:-

[
{
"num":"000147"
},
{
"num":"100297"
},
]

Output:-

[
{
"num": "147"
},
{
"num": "297"
}
]

Dataweave Code:-

import last from dw::core::Strings
- -
payload map(v,k)-> (num: v.num last 3)

7. Count the Number of Characters in a String

Input:-

{
"string":"12shubham245fc"
}

Output:-

9

Dataweave Code:-

import countCharactersBy,isAlpha from dw::core::Strings
- -
payload.string countCharactersBy isAlpha($)

8. Hamming Distance:- The Hamming distance between two equal-length strings of symbols is the number of positions at which the corresponding symbols are different

Input:-

[{
"hola":"bola"
}
{
"chao":"figo"
}
]

Output:-

[
{
"Hamming Distance": 1
},
{
"Hamming Distance": 3
}
]

Dataweave Code:-

import hammingDistance from dw::core::Strings
- -
(payload map(v,k)->{
(v mapObject(value,key,index)->{
"Hamming Distance":key hammingDistance value
})
})

9. Find Number of Object or Key-Value Pairs in an Array

Input:-

[{
"k1":"v1"
},
{
"k2":"v2"
}
]

Output:-

2

Dataweave Code:-

sizeOf(payload)

10. Generate Random Numbers

Input:-

[10]

Output:-

[89,26,21,77,80,31,24,13,54,55]

Dataweave Code:-

(1 to payload[0]) map randomInt(100)

📢 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/

--

--

Shubham Chaurasia

MuleSoft Ambassador | Mule Certified Architect & Developer | 11x Salesforce| 4x AWS | 2x GCP | 2x Solace | 2xAzure | https://linkedin.com/in/shubhamchaurasia1/