Dataweave Series for Practice & Interview Part-9
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.
- Given a list of numbers, write a function to find the longest increasing subsequence
Input:-
Output:-
Dataweave Code:-
2. Given a string, write a function to find the longest substring without repeating characters.
Input:-
[]
Output:-
Dataweave Code:-
3.
Input:-
{
"startcount": "0",
"pagesize": "35",
"total records": "100"
}
Output:-
[
{
"endrecords": 35,
"startindex": 0
},
{
"endrecords": 70,
"startindex": 36
},
{
"endrecords": 100,
"startindex": 71
}
]
Dataweave Code:-
%dw 2.0
output application/json
var arr=[]
fun fn(start,ps,end)=
if (end<=0) arr
else flatten(arr + {
"endrecords": if (((payload.pagesize)*start)<payload."total records") ((payload.pagesize)*start)+payload.startcount else payload."total records" as Number,
"startindex": ps}+ fn((start+1),((payload.pagesize)*start)+1,end-payload.pagesize))
---
fn(1,payload.startcount as Number,payload."total records")
4.
Input:-
{
"date": "Apr-22",
"Range": {
"1": 0,
"2": 67,
"3": 198,
"Z": 5
},
"date": "Dec-21",
"severity": {
"1": 2,
"2": 53,
"3": 154,
"R": 1
},
"date": "Jan-22",
"Range": {
"1": 0,
"2": 47,
"3": 118,
"Z": 4
},
"date": "Oct-21",
"Range": {
"1": 0,
"2": 46,
"3": 123,
"Z": 2
},
"date": "Jul-22",
"Range": {
"1": 0,
"2": 66,
"3": 136,
"Z": 3
},
"date": "Mar-22",
"Range": {
"1": 0,
"2": 70,
"3": 215,
"Z": 5
},
"date": "Sep-21",
"Range": {
"1": 0,
"2": 53,
"3": 109,
"Z": 5
},
"date": "Nov-21",
"Range": {
"1": 1,
"2": 64,
"3": 138,
"Z": 9
},
"date": "May-22",
"Range": {
"1": 0,
"2": 6,
"3": 164,
"Z": 6
},
"date": "Feb-22",
"Range": {
"1": 0,
"2": 47,
"3": 156,
"Z": 3
},
"date": "Jun-22",
"Range": {
"1": 0,
"2": 74,
"3": 171,
"Z": 6
}
}
Output:-
{
"date": "Sep-21",
"Range": {
"1": 0,
"2": 53,
"3": 109,
"Z": 5
},
"date": "Oct-21",
"Range": {
"1": 0,
"2": 46,
"3": 123,
"Z": 2
},
"date": "Nov-21",
"Range": {
"1": 1,
"2": 64,
"3": 138,
"Z": 9
},
"date": "Dec-21",
"severity": {
"1": 2,
"2": 53,
"3": 154,
"Z": 1
},
"date": "Jan-22",
"Range": {
"1": 0,
"2": 47,
"3": 118,
"Z": 4
},
"date": "Feb-22",
"Range": {
"1": 0,
"2": 47,
"3": 156,
"Z": 3
},
"date": "Mar-22",
"Range": {
"1": 0,
"2": 70,
"3": 215,
"Z": 5
},
"date": "Apr-22",
"Range": {
"1": 0,
"2": 67,
"3": 198,
"Z": 5
},
"date": "May-22",
"Range": {
"1": 0,
"2": 6,
"3": 164,
"Z": 6
},
"date": "Jun-22",
"Range": {
"1": 0,
"2": 74,
"3": 171,
"Z": 6
},
"date": "Jul-22",
"Range": {
"1": 0,
"2": 66,
"3": 136,
"Z": 3
}
}
Dataweave Code:-
%dw 2.0
output application/json
import divideBy from dw::core::Objects
var dates = payload..*date orderBy (daysBetween (now() as Date , ("01-"++ $)as Date {format:"d-MMM-yy"}) )
---
flatten(dates map ((item, index) -> (payload divideBy 2)[?($.date ==item)]))
5.
Input:-
[
{
"product":"Laptop",
"region":"North",
"sales":[
{
"month":"January",
"amount":1200
},
{
"month":"Febraury",
"amount":1500
},
{
"month":"March",
"amount":1800
}
]
},
{
"product":"smartphone",
"region":"North",
"sales":[
{
"month":"January",
"amount":800
},
{
"month":"Febraury",
"amount":1000
},
{
"month":"March",
"amount":1200
}
]
},
{
"product":"Laptop",
"region":"South",
"sales":[
{
"month":"January",
"amount":1000
},
{
"month":"Febraury",
"amount":1200
},
{
"month":"March",
"amount":1500
}
]
},
{
"product":"smartphone",
"region":"North",
"sales":[
{
"month":"January",
"amount":1000
},
{
"month":"Febraury",
"amount":1200
},
{
"month":"March",
"amount":1500
}
]
}
]
Output:-
[
{
"region":"North",
"totalsales":7500,
"products":{
"Laptop":{
"totalsales":4500,
"averagesales":1500
},
"smartphone":{
"totalsales":3000,
"averagesales":1000
}
}
}
]
Dataweave Code:-
%dw 2.0
output application/json
var Regions =["North", "South"]
---
(((payload groupBy $.region) pluck $ map(data, index) ->{
region: data.region[0],
totalSales: sum((flatten(data.sales)).amount),
products: data groupBy $.product mapObject ((value, key,index) -> {
(key): {
totalSales: sum(flatten (value.sales).amount),
averageSales: sum(flatten (value.sales).amount)/sizeOf
(flatten(value.sales).month)
}
})
}) filter ((item, index) -> Regions contains item.region )
)
6.
📢 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:-
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/