Naga Raj
Naga Raj

Reputation: 19

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

I am working on a mule. While working I came across a scenario, where I want to generate all possible combinations of values from the incoming JSON array list (3-4 array objects). The values under the JSON array list are dynamic.

I want to generate the output in JSON format. I want to achieve this only using data weave. Please help me how to achieve this in mule 4 using datawevae.

Below is my JSON input and required JSON output.

Note: Json array lists are dynamic.

Input JSON ARRAY:

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

Output:

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

I could able to write data weave code for 2 array objects using flatten and map, but more no-of array objects combinations.

Upvotes: 0

Views: 184

Answers (2)

weissj
weissj

Reputation: 135

I'm pretty sure Salim Khan's answer is more performant, but here's the map and flatten solution in case it's useful to anyone.



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

Upvotes: 1

Salim Khan
Salim Khan

Reputation: 4303

Try with this approach.

Input

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

Script

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

Output

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

Upvotes: 1

Related Questions