如何在DataWeave 1.0中按多条件循环处理产品Payload
DataWeave 1.0 按条件处理产品Payload
处理规则
- 当productId为1003的
isOwned为true时,将productId为1004、1002、1001、1000的newPrice设为null,hasSpecialPrice设为false - 当productId为1004的
isOwned为true时,将productId为1002、1001、1000的newPrice设为null,hasSpecialPrice设为false - 当productId为1002的
isOwned为true时,将productId为1001、1000的newPrice设为null,hasSpecialPrice设为false
示例输入Payload
{"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}]}
当productId=1003的isOwned=true时的预期输出
{"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}]}
DataWeave 1.0 实现脚本
%dw 1.0 %output application/json --- { products: payload.products map (product) -> { (product - "flavors"), flavors: product.flavors map (flavor) -> { (flavor - "newPrice", "hasSpecialPrice"), newPrice: do { var is1003Owned = payload.products filter ($.id == "1003") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var is1004Owned = payload.products filter ($.id == "1004") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var is1002Owned = payload.products filter ($.id == "1002") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var shouldNullify = (is1003Owned and (flavor.productId == "1000" or flavor.productId == "1001" or flavor.productId == "1002" or flavor.productId == "1004")) or (is1004Owned and (flavor.productId == "1000" or flavor.productId == "1001" or flavor.productId == "1002")) or (is1002Owned and (flavor.productId == "1000" or flavor.productId == "1001")) if (shouldNullify) null else flavor.newPrice }, hasSpecialPrice: do { var is1003Owned = payload.products filter ($.id == "1003") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var is1004Owned = payload.products filter ($.id == "1004") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var is1002Owned = payload.products filter ($.id == "1002") flatMap $.flavors filter ($.entitlement.isOwned == true) != [] var shouldDisable = (is1003Owned and (flavor.productId == "1000" or flavor.productId == "1001" or flavor.productId == "1002" or flavor.productId == "1004")) or (is1004Owned and (flavor.productId == "1000" or flavor.productId == "1001" or flavor.productId == "1002")) or (is1002Owned and (flavor.productId == "1000" or flavor.productId == "1001")) if (shouldDisable) false else flavor.hasSpecialPrice } } } }
脚本逻辑说明
- 先通过
filter和flatMap判断三个触发条件(1003/1004/1002的isOwned是否为true) - 遍历每个产品的flavors字段,判断当前flavor的productId是否属于需要修改的范围
- 根据判断结果,决定是否将
newPrice设为null、hasSpecialPrice设为false,否则保留原字段值
内容的提问来源于stack exchange,提问作者Niharika Tangella
相关产品推荐
相关产品推荐

