如何在 mulesoft 中映射多个数组元素

问题描述 投票:0回答:1

我们必须映射多个数组对象以形成结果数组。

输入:

{
    "data": {
    "contents": {
    "desserts": [
    {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters":
            
                    [
                        { "id": "1001", "type": "Regular" },
                        { "id": "1002", "type": "Chocolate" }
                    ],
        "topping":
            [
                
            ],
        "base": [
            {
                "id": "001",
                "type": "wheat"
            },
            {
                "id": "003",
                "type": "corn"
            }
        ]
    },
    {
        "id": "0002",
        "type": "donut",
        "name": "Raised",
        "ppu": 0.55,
        "batters":
            [
                        { "id": "1004", "type": "Devil's Food" }
                    ],
        "topping":
            [
                { "id": "5001", "type": "None" },
                { "id": "5002", "type": "Glazed" }
            ],
        "base": [
            {
                "id": "005",
                "type": "cashew"
            }
        ]
    }
]
}
}
}

预期输出:

[
  {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batterid": "1001",
    "battertype": "Regular",
    "toppingid": "",
    "toppingtype": "",
    "baseid": "001",
    "basetype": "wheat"
  },
  {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batterid": "1002",
    "battertype": "Chocolate",
    "toppingid": "",
    "toppingtype": "",
    "baseid": "001",
    "basetype": "wheat"
  },
  {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batterid": "1001",
    "battertype": "Regular",
    "toppingid": "",
    "toppingtype": "",
    "baseid": "003",
    "basetype": "corn"
  },
  {
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batterid": "1002",
    "battertype": "Chocolate",
    "toppingid": "",
    "toppingtype": "",
    "baseid": "003",
    "basetype": "corn"
  },
  {
    "id": "0002",
    "type": "donut",
    "name": "Raised",
    "ppu": 0.55,
    "batterid": "1004",
    "battertype": "Devil's Food",
    "toppingid": "5001",
    "toppingtype": "None",
    "baseid": "005",
    "basetype": "cashew"
  },
  {
    "id": "0002",
    "type": "donut",
    "name": "Raised",
    "ppu": 0.55,
    "batterid": "1004",
    "battertype": "Devil's Food",
    "toppingid": "5002",
    "toppingtype": "Glazed",
    "baseid": "005",
    "basetype": "cashew"
  }
]

以上输入有效负载将通过面糊、顶部和基础阵列进行映射。对于每个击球手对象、顶部对象和基础对象,重复数组中的对象。如果其中任何一个为空,则应在输出中显示其他数组。如果我们使用reduce,如果该数组为空,则不会考虑整个输出对象。

我尝试使用地图制作甜点,然后减少面糊,再次使用地图制作配料,但这并没有给出预期的结果。还需要为基础添加另一个地图功能。所以没成功。

arrays dataweave mulesoft
1个回答
0
投票

我发现这个问题的复杂性在于需要对所有键进行重命名。也许有一个最简单的解决方案。要获得组合,只需使用嵌套映射实现数组乘法即可。

%dw 2.0
output application/json

fun multiply(dessert, name1, a1, name2, a2)=
do {
    var b1=if (isEmpty(a1)) [{ (name1++"id"):"", (name1++"type"):""}] else (a1 map ($ mapObject (name1++ $$ as String): $))
    var b2=if (isEmpty(a2)) [{ (name2++"id"):"", (name2++"type"):""}] else (a2 map ($ mapObject (name2++ $$ as String): $)) 
    ---
    b1 flatMap ((item, index) -> b2 map (($ ++ item) update {
            case id at .id! -> dessert.id
            case t at ."type"! -> dessert."type"
            case name at .name! -> dessert.name
            case ppu at .ppu! -> dessert.ppu
        })  
    ) 
}
---
payload.data.contents.desserts flatMap 
    multiply($, "", multiply($, "batter", $.batters, "base", $.base), "topping", $.topping)

输出:

[
  {
    "toppingid": "",
    "toppingtype": "",
    "baseid": "001",
    "basetype": "wheat",
    "batterid": "1001",
    "battertype": "Regular",
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55
  },
  {
    "toppingid": "",
    "toppingtype": "",
    "baseid": "003",
    "basetype": "corn",
    "batterid": "1001",
    "battertype": "Regular",
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55
  },
  {
    "toppingid": "",
    "toppingtype": "",
    "baseid": "001",
    "basetype": "wheat",
    "batterid": "1002",
    "battertype": "Chocolate",
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55
  },
  {
    "toppingid": "",
    "toppingtype": "",
    "baseid": "003",
    "basetype": "corn",
    "batterid": "1002",
    "battertype": "Chocolate",
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55
  },
  {
    "toppingid": "5001",
    "toppingtype": "None",
    "baseid": "005",
    "basetype": "cashew",
    "batterid": "1004",
    "battertype": "Devil's Food",
    "id": "0002",
    "type": "donut",
    "name": "Raised",
    "ppu": 0.55
  },
  {
    "toppingid": "5002",
    "toppingtype": "Glazed",
    "baseid": "005",
    "basetype": "cashew",
    "batterid": "1004",
    "battertype": "Devil's Food",
    "id": "0002",
    "type": "donut",
    "name": "Raised",
    "ppu": 0.55
  }
]
© www.soinside.com 2019 - 2024. All rights reserved.