使用数据编织从 three/four json 数组列表生成所有可能的组合

Generating all possible combination from three/four json array list using dataweave

我正在研究骡子。在工作时,我遇到了一个场景,我想从传入的 JSON 数组列表(3-4 个数组对象)中生成所有可能的值组合。 JSON数组列表下的值是动态的。

我想生成 JSON 格式的输出。我只想使用数据编织来实现这一点。请帮助我如何使用 datawevae 在 mule 4 中实现这一点。

下面是我的 JSON 输入和要求的 JSON 输出。

注意:Json数组列表是动态的。

输入JSON数组:

    {
  "airlines": [
    {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    }
  ],
  "hotels": [
    {
      "hotelId": "tj23a",
      "hotelName": "Taj",
      "hotelCost": 600
    },
    {
      "hotelId": "f",
      "hotelName": "fdg",
      "hotelCost": 600
    }
  ],
  "cabs": [
    {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    }
  ]
}

输出:

    {
  "itenary 1": {
    "hotelDetails": {
      "hotelId": "tj23a",
      "hotelName": "Taj",
      "hotelCost": 6000
    },
    "airlines": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 1700
    },"cabs":
        {
        "cabId": "ola2312",
        "cabName":"Ola",
        "Cost":"600"    
    },
    "TripPackage" : 8300
  },
  "itenary 2": {
    "hotelDetails": {
      "hotelId": "Mnva",
      "hotelName": "Minvera",
      "hotelCost": 1600
    },
    "airlines": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 1700
    },
    "cabs":
        {
        "cabId": "ola2312",
        "cabName":"Ola",
        "Cost":"600"    
    },
    "TripPackage" : 3900
  },
  "itenary 3": {
    "hotelDetails": {
      "hotelId": "tj23a",
      "hotelName": "Taj",
      "hotelCost": 6000
    },
    "airlines": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 1600
    },
    "cabs":
        {
        "cabId": "ola2312",
        "cabName":"Ola",
        "Cost":"600"    
    },
    "TripPackage" : 8200
  },
  "itenary 4": {
    "hotelDetails": {
      "hotelId": "Mnva",
      "hotelName": "Minvera",
      "hotelCost": 1600
    },
    "airlines": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 1600
    },
    "cabs":
        {
        "cabId": "ola2312",
        "cabName":"Ola",
        "Cost":"600"    
    },
    "TripPackage" : 3800
  }
}

我可以使用 flatten 和 map 为 2 个数组对象编写数据编织代码,但更多的是无数组对象组合。

试试这个方法。

输入

{
  "airlines": [
    {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    }

  ],
  "hotels": [
    {
      "hotelId": "tj23a",
      "hotelName": "Taj",
      "hotelCost": 600
    },
    {
      "hotelId": "f",
      "hotelName": "fdg",
      "hotelCost": 600
    },
     {
      "hotelId": "g",
      "hotelName": "fasddg",
      "hotelCost": 650
    }

  ],
  "cabs": [
    {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
     {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    }
  ]
}

脚本

%dw 2.0
output application/json
var totalCombinations = sizeOf(payload.airlines) * sizeOf(payload.hotels) * sizeOf(payload.cabs)
---
{(1 to totalCombinations map   {
    "itenary $($$+1)" : 
             hotelDetails:  if (($$) > sizeOf(payload.hotels) -1) payload.hotels[($$) mod sizeOf(payload.hotels)]  else payload.hotels[($$)] ,
             airlineDetails: if (($$) > sizeOf(payload.airlines) -1) payload.airlines[($$) mod sizeOf(payload.airlines)]  else payload.airlines[($$)],
             cabDetails : if (($$) > sizeOf(payload.cabs) -1) payload.cabs[($$) mod sizeOf(payload.cabs)]  else payload.cabs[($$)],
            TripPackage: ((if (($$) > sizeOf(payload.hotels) -1) payload.hotels[($$) mod sizeOf(payload.hotels)].hotelCost as Number  else payload.hotels[($$)].hotelCost as Number) + (if (($$) > sizeOf(payload.airlines) -1) payload.airlines[($$) mod sizeOf(payload.airlines)].airwayCost as Number  else payload.airlines[($$)].airwayCost as Number) + if (($$) > sizeOf(payload.cabs) -1) payload.cabs[($$) mod sizeOf(payload.cabs)].Cost as Number  else payload.cabs[($$)].Cost as Number)
})}

输出

[
  {
    "itenary 1": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1900
  },
  {
    "itenary 2": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2200
  },
  {
    "itenary 3": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 2850
  },
  {
    "itenary 4": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2300
  },
  {
    "itenary 5": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1800
  },
  {
    "itenary 6": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 3250
  },
  {
    "itenary 7": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1900
  },
  {
    "itenary 8": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2200
  },
  {
    "itenary 9": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 2850
  },
  {
    "itenary 10": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2300
  },
  {
    "itenary 11": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1800
  },
  {
    "itenary 12": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 3250
  },
  {
    "itenary 13": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1900
  },
  {
    "itenary 14": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2200
  },
  {
    "itenary 15": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 2850
  },
  {
    "itenary 16": {
      "hotelDetails": {
        "hotelId": "tj23a",
        "hotelName": "Taj",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0520k",
      "airlineName": "Kingfisher",
      "airwayCost": 700
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 2300
  },
  {
    "itenary 17": {
      "hotelDetails": {
        "hotelId": "f",
        "hotelName": "fdg",
        "hotelCost": 600
      }
    },
    "airlineDetails": {
      "airlineId": "0620i",
      "airlineName": "indigo",
      "airwayCost": 600
    },
    "cabDetails": {
      "cabId": "ola2312",
      "cabName": "Ola",
      "Cost": "600"
    },
    "TripPackage": 1800
  },
  {
    "itenary 18": {
      "hotelDetails": {
        "hotelId": "g",
        "hotelName": "fasddg",
        "hotelCost": 650
      }
    },
    "airlineDetails": {
      "airlineId": "0720i",
      "airlineName": "Emirates",
      "airwayCost": 1600
    },
    "cabDetails": {
      "cabId": "uber22",
      "cabName": "Uber",
      "Cost": "1000"
    },
    "TripPackage": 3250
  }
]

我很确定 Salim Khan 的回答性能更好,但这里是地图和展平解决方案,以防对任何人有用。



    %dw 2.0
    output application/json
    var hotelCount = sizeOf(payload.hotels)
    var cabCount = sizeOf(payload.cabs)
    fun itineraryNum(ia, ih, ic) = ia * hotelCount * cabCount + ih * cabCount + ic + 1
    
    ---
    flatten(flatten(
        payload.airlines map ((a, ia) -> 
            payload.hotels map ((h, ih ) -> 
                payload.cabs map ((c, ic) -> {
                    ("itinerary " ++ itineraryNum(ia, ih, ic) as String): {
                        hotelDetails: {
                            hotelId: h.hotelId,
                            hotelName: h.hotelName,
                            hotelCost: h.hotelCost
                        },
                        airlineDetails: {
                            airlineId: a.airlineId,
                            airlineName: a.airlineName,
                            airwayCost: a.airwayCost 
                        },
                        cabs: {
                            cabId: c.cabId,
                            cabName: c.cabName,
                            Cost: c.Cost
                        },
                        TripPackage: h.hotelCost + a.airwayCost + c.Cost
                    }
                }) 
            )
        )
    ))