使用dataweave从三个/四个json数组列表生成所有可能的组合



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

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

下面是我的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
}
}) 
)
)
))

相关内容

  • 没有找到相关文章

最新更新