【问题标题】:Generating all possible combination from three/four json array list using dataweave使用dataweave从三/四json数组列表生成所有可能的组合
【发布时间】:2021-10-08 04:41:06
【问题描述】:

我正在制作骡子。在工作时,我遇到了一个场景,我想从传入的 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 个数组对象编写数据编织代码,但可以使用更多的数组对象组合。

【问题讨论】:

    标签: mule dataweave mule4 mule-esb


    【解决方案1】:

    试试这种方法。

    输入

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

    【讨论】:

      【解决方案2】:

      我很确定 Salim Khan 的答案更高效,但这里是 map and flatten 解决方案,以防它对任何人有用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        • 1970-01-01
        • 1970-01-01
        • 2014-10-22
        • 2016-08-16
        • 2017-09-15
        相关资源
        最近更新 更多