**I have below sample payload, where I need below conditions to meet and get the same payload back using DW 1.0
If isOwned = true for productId = 1003 Then show newPrice = null and hasSpecialPrice = false for 1004,1002,1001,1000
If isOwned = true for productId = 1004 Then show newPrice = null and hasSpecialPrice = false for 1002,1001,1000
If isOwned = true for productId = 1002 Then show newPrice = null and hasSpecialPrice = false for 1001,1000
**
{
"products":[
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"0.00",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1000"
}
],
"id":"1000",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"6.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1001"
}
],
"id":"1001",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"5.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1002"
}
],
"id":"1002",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":true
},
"locale":"en_US",
"price":"3.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1003"
}
],
"id":"1003",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"2.99",
"newPrice":"1.99",
"hasSpecialPrice":true,
"productId":"1004"
}
],
"id":"1004",
"enabled":null
}
]
}
Expected Output for condition isOwned = true for productId = 1003 Then show values newPrice = null and hasSpecialPrice = false for 1004,1002,1001,1000:
{
"products":[
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"0.00",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1000"
}
],
"id":"1000",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"6.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1001"
}
],
"id":"1001",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"5.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1002"
}
],
"id":"1002",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":true
},
"locale":"en_US",
"price":"3.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1003"
}
],
"id":"1003",
"enabled":null
},
{
"flavors":[
{
"features":null,
"entitlement":{
"isOwned":false
},
"locale":"en_US",
"price":"2.99",
"newPrice":null,
"hasSpecialPrice":false,
"productId":"1004"
}
],
"id":"1004",
"enabled":null
}
]
}
In DataWeave 1.0 you can use when/otherwise for simple conditions and pattern matching with match
for more complex ones. To avoid putting too much logic in the main map, I created a variable that returns which of the rules is triggered and a function that updates each element. The object updateWhich
is for identifying which elements needs to be updated based on the id triggered.
%dw 1.0
%output application/json
%var updateWhich={
"1002": ["1000", "1001"],
"1003": ["1000", "1001", "1002", "1004"],
"1004": ["1000", "1001", "1002"]
}
%var updateId=(payload.products map ( $ match {
o when o.id == "1002" and o.flavors.entitlement.isOwned[0] -> "1002",
o when o.id == "1003" and o.flavors.entitlement.isOwned[0] -> "1003",
o when o.id == "1004" and o.flavors.entitlement.isOwned[0] -> "1004",
default -> null
}) filter ( $ != null ))[0]
%function updateProduct(o) o - "flavors" ++ {flavors: [o.flavors[0] - "newPrice" - "hasSpecialPrice" ++ {newPrice: null, hasSpecialPrice: false} ]}
---
products: payload.products map ( updateProduct($) when (updateWhich[updateId] != null) otherwise $)