简体   繁体   English

Mule4 Dataweave 转换

[英]Mule4 Dataweave transformation

I need to transform the below JSON我需要改造以下 JSON

Input:-输入:-

{
    "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}
        ]
}

The output that I want is我要的output是

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",
    },
}

Explanantion:-说明:-

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

For example if Batter Name is "Chocolate", then there will be 6 toppings for Chocolate batter and so on for each batter.例如,如果 Batter Name 是“Chocolate”,那么巧克力面糊将有 6 个配料,每个面糊依此类推。 So total batter number is 4 and topping is 8, I want 32 items in the final output所以击球手总数是 4,浇头是 8,最后我想要 32 个项目 output

You basically need a cross join on toppings and batters.您基本上需要在浇头和面糊上进行交叉连接。 You can use join from dw::core::Arrays to do that.您可以使用join from dw::core::Arrays来执行此操作。 It accepts the 2 arrays as input along with two joining criteria (which are inline functions).它接受 2 arrays 作为输入以及两个连接条件(内联函数)。 For that you can just pass a function that always returns true (or any other static value but it should be same in both criteria functions) so the function will merge every item with every item, and you will get all combos possible.为此,您可以只传递始终返回true的 function(或任何其他 static 值,但它在两个标准函数中应该相同),因此 function 会将每个项目与每个项目合并,您将获得所有可能的组合。

I noticed that the names of the snack after combining is not very straight forward, so I crated a separated function for that.我注意到合并后的零食名称不是很直接,所以我为此单独创建了一个 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