Dataweave Series for Practice & Interview Part-1 - 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.
# Dataweave Code Snippets
## 1. Filter Odd & Even Numbers from an Array
**Input:**
[0,1,2,3,4,5,6,7,8,9]
**Output:**
```json
{
"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)
}
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
}
)
Input:
{
"Reverse": "Dataweave 2.0 is Cool"
}
Output:
"Cool is 2.0 Dataweave"
Dataweave Code:
(payload.Reverse splitBy(' '))[-1 to 0] joinBy(' ')
Input:
[
{
"Product":"A",
"Year":"2020"
},
{
"Product":"B",
"Year":"2019"
}
]
Output:
[
{
"Product": "A",
"Year": "2020"
}
]
Dataweave Code:
payload filter $.Year>2019
Input:
[0,1,2,3,4,5,6,7,8,9]
Output:
[0,2,4,6,8]
Dataweave Code:
payload filter isEven($)
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)
Input:
{
"string":"12shubham245fc"
}
Output:
9
Dataweave Code:
import countCharactersBy,isAlpha from dw::core::Strings
payload.string countCharactersBy isAlpha($)
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
})
})
Input:
[{
"k1":"v1"
},
{
"k2":"v2"
}
]
Output:
2
Dataweave Code:
sizeOf(payload)
Input:
[10]
Output:
[89,26,21,77,80,31,24,13,54,55]
Dataweave Code:
(1 to payload[0]) map randomInt(100)
Feel free to let me know if you need any further assistance!