【问题标题】:Mule4 Dataweave transformationMule4 Dataweave 转换
【发布时间】:2022-11-18 00:54:41
【问题描述】:

我需要转换下面的 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}
        ]
}

我想要的输出是

输出 :-

{
    "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”:{ “重量”:500(电池重量), “单位”:“公斤”(硬编码), “价格”:34.1(面糊价格+打顶价格), “单位”:“$/kg”(硬编码, }

例如,如果 Batter Name 是“Chocolate”,那么巧克力面糊将有 6 个配料,每个面糊依此类推。所以总击球手数是 4 和 topping 是 8 ,我想要最终输出中的 32 项

【问题讨论】:

  • 你好。欢迎来到堆栈溢出。您分享的 JSON 非常大,需要读者花很多精力才能理解其中的逻辑。请添加转换背后的逻辑是什么,以及您是否尝试过任何内容。
  • 嗨@aparajitatiwary。输出与输入没有明显的关系。在您阐明如何将输入转换为输出的逻辑之前,不可能提供答案。然后是在 DataWeave 中实现该逻辑的问题。如果您在添加解释后仍然有问题,那么回答是合适的。请阅读有关如何提问的 Stackoverflow 指南stackoverflow.com/help/how-to-ask
  • 添加说明

标签: mule transformation dataweave mulesoft mule4


【解决方案1】:

您基本上需要在浇头和面糊上进行交叉连接。您可以使用 dw::core::Arrays 中的 join 来做到这一点。它接受 2 个数组作为输入以及两个连接条件(内联函数)。为此,您只需传递一个始终返回 true 的函数,这样该函数就会将每个项目与每个项目合并,您将获得所有可能的组合。

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

%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"
    }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-22
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2019-08-23
    • 2020-04-13
    • 2021-12-23
    相关资源
    最近更新 更多