Search code examples
muledataweave

How to map nodes in an array in Mule 3


I am trying to get value from offers.offer.productOffers.productOffer. Required uri and id from offers.offer.productOffers.productOffer. I am using DataWeave 1.0. Not able to return the required value using map. Returning a null value.

Sample payload

{
   "offers":{
      "uri":"https",
      "offer":[
         {
            "uri":"https",
            "id":454645464,
            "name":"Shipping -Test",
            "policyName":null,
            "type":"Shipping Order Value",
            "image":null,
            "trigger":"Always Triggered",
            "salesPitch":[
               null,
               null,
               null,
               null
            ],
            "customAttributes":{
               
            },
            "productOffers":{
               "uri":"https"
            },
            "categoryOffers":{
               
            },
            "offerBundleGroups":{
               
            }
         },
         {
            "uri":"https",
            "id":63501325815,
            "customAttributes":{
               
            },
            "productOffers":{
               "uri":"https",
               "productOffer":[
                  {
                     "uri":"https",
                     "id":9877897987
                  }
               ]
            }
         }
      ]
   }
}

Solution

  • You are not able to access those values directly because payload.offers.offer s an array, and productOffer is another array.

    So payload.offers.offer.productOffers.productOffer will return an array of arrays:

    [
      [
        {
          "uri": "https",
          "id": 9877897987
        }
      ]
    ]
    

    You could use the flatten operator to remove one level of nested arrays flatten(payload.offers.offer.productOffers.productOffer):

    [
      {
        "uri": "https",
        "id": 9877897987
      }
    ]
    

    From that it depends on how you want to process the list of uri and id elements.