繁体   English   中英

Mule4 Dataweave 转换

[英]Mule4 Dataweave transformation

我需要改造以下 JSON

输入:-

{
    "type": "donut",
    "weight-unit": "lb",
    "price-unit": "$/lb",
    "price": 10.75,
    "batters":
        {
            "batter":
                [
                    { "id": "10011", "type": "Original","weight": 500},
                    { "id": "10021", "type": "Chocolate","weight": 200, "price": 11.75 },
                    { "id": "10031", "type": "Blueberry", "weight": 250, "price": 11.75  },
                    { "id": "10041", "type": "Devil's Food", "weight": 150}
                ]
        },
    "topping":
        [
            { "id": "50011", "type": "None", "price": 0 },
            { "id": "50021", "type": "Glazed", "price": 45.23},
            { "id": "50051", "type": "Sugar", "price": 34.1},
            { "id": "50071", "type": "Powdered Sugar", "price": 21.11},
            { "id": "50061", "type": "Chocolate with Sprinkles", "price": 34.43 },
            { "id": "50031", "type": "Chocolate", "price": 87.40},
            { "id": "50041", "type": "Maple", "price": 64.11}
        ]
}

我要的output是

Output:-

{
    "type": "donut",
    "ChocolateFlavoredGlazedDonut" : {
        "weight": 200,
        "unit": "kg",
        "price": 56.98,
        "unit": "$/kg",
    },
    "ChocolateFlavoredSprinklesDonut" : {
        "weight": 200,
        "unit": "kg",
        "price": 46.18,
        "unit": "$/kg",
    },
    "BlueberryFlavoredSugarDonut" : {
        "weight": 250,
        "unit": "kg",
        "price": 45.85,
        "unit": "$/kg",
    },
    "OriginalGlazedDonut" : {
        "weight": 500,
        "unit": "kg",
        "price": 45.23,
        "unit": "$/kg",
    },
        "OriginalMapleDonut" : {
        "weight": 500,
        "unit": "kg",
        "price": 64.11,
        "unit": "$/kg",
    },
        "OriginalSugarDonut" : {
        "weight": 500,
        "unit": "kg",
        "price": 34.1,
        "unit": "$/kg",
    },
}

说明:-

"BatterName + ToppingName": { "weight": 500(batter weight), "unit": "kg"(硬编码), "price": 34.1(bat price + topping price), "unit": "$/kg “(硬编码,}

例如,如果 Batter Name 是“Chocolate”,那么巧克力面糊将有 6 个配料,每个面糊依此类推。 所以击球手总数是 4,浇头是 8,最后我想要 32 个项目 output

您基本上需要在浇头和面糊上进行交叉连接。 您可以使用join from dw::core::Arrays来执行此操作。 它接受 2 arrays 作为输入以及两个连接条件(内联函数)。 为此,您可以只传递始终返回true的 function(或任何其他 static 值,但它在两个标准函数中应该相同),因此 function 会将每个项目与每个项目合并,您将获得所有可能的组合。

我注意到合并后的零食名称不是很直接,所以我为此单独创建了一个 function。

%dw 2.0
import join from dw::core::Arrays
import capitalize from dw::core::Strings
output application/json

fun getComboName(batterName, toppingName, snackType) = 
    capitalize(batterName) 
    ++ (if(lower(batterName) != "original")("Flavoured") else "")
    ++ (if(lower(toppingName) != "none") capitalize((toppingName splitBy " ")[-1]) else "")
    ++ capitalize(snackType) 
---
join(
    payload.batters.batter,
    payload.topping,
    (a) -> true,
    (a) -> true
)
reduce ((combo, acc={"type": payload."type"}) -> {
    (acc),
    (getComboName(combo.l."type", combo.r."type", payload."type")): {
        weight: combo.l.weight,
        unit: "kg",
        price: (combo.l.price default 0) + (combo.r.price default 0),
        unit: "\$/kg"
    }
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM