You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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
            }
        }
    }
}

脚本逻辑说明

  1. 先通过filter和flatMap判断三个触发条件(1003/1004/1002的isOwned是否为true)
  2. 遍历每个产品的flavors字段,判断当前flavor的productId是否属于需要修改的范围
  3. 根据判断结果,决定是否将newPrice设为null、hasSpecialPrice设为false,否则保留原字段值

内容的提问来源于stack exchange,提问作者Niharika Tangella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 08:40:41