Search code examples
dataweavemulesoft

Dataweave 2.0: Transform nested Arrays to flat list with parent details


I have a list of productFeatures inside an Array of products.
I need to create a list of products for each feature inside productFeatures array.

I am currently using Dataweave 2.0 to achieve this transformation.

Tried to use map() paylaod.pricingPlan.productFeatures but it is returning complete list of productFeatures. I need to have parent details for each list.

Input:

[
    {
        "parentKey": "cars",
        "key": "com.automotive.cars.wheels",
        "description": "descirption for cars",
        "productType": "parts",
        "billingType": "PERPETUAL",
        "pricingPlanModel": true,
        "addOn": true,
        "monthlyPricingPlan": null,
        "annualPricingPlan": {
            "pricingPlanId": 3098756839,
            "pricingPlanUuid": "0c99989e-9877-9845-4357-7655ab821654",
             "billingPeriod": "Annual",
            "productKey": "com.automotive.cars.wheels.black",
            "productDescription": "product descirptions for wheels",
            "productFeatures": [
                {
                    "featureKey": "feature1",
                    "featureDescription": "featureDescription1",
                    "unitPricingPolicy": "TIERED",
                    "unitCountLimit": 10,
                    "amount": 10,
                    "otherCurrencyAmounts": {},
                    "featureLabel": "featureLabel",
                    "licenseFeatureLabel": "featureLicenser"
                }
            ]
        },
        "productDescriptionWithVendorName": "Issue for wheels"
    },
    {
        "parentKey": "bike",
         "key": "com.automotive.cars.bike",
        "description": "descirption for bike",
        "productType": "parts",
        "billingType": "PERPETUAL",
        "pricingPlanModel": true,
        "addOn": true,
        "monthlyPricingPlan": null,
        "annualPricingPlan": {
            "pricingPlanId": 3098762339,
            "pricingPlanUuid": "0c99989e-9877-9845-4357-7655a0981654",
             "billingPeriod": "Annual",
            "productKey": "com.automotive.cars.bike.black",
            "productDescription": "product descirptions for bike",
            "productFeatures": [

                {
                    "featureKey": "feature2",
                    "featureDescription": "featureDescription2",
                    "unitPricingPolicy": "TIERED",
                    "unitCountLimit": 20,
                    "amount": 20,
                    "otherCurrencyAmounts": {},
                    "featureLabel": "featureLabel2",
                    "licenseFeatureLabel": "featureLicenser2"
                },
                   {
                    "featureKey": "feature3",
                    "featureDescription": "featureDescription3",
                    "unitPricingPolicy": "TIERED",
                    "unitCountLimit": 30,
                    "amount": 30,
                    "otherCurrencyAmounts": {},
                    "featureLabel": "featureLabel3",
                    "licenseFeatureLabel": "featureLicenser3"
                }

            ]
        },
        "productDescriptionWithVendorName": "Issue for bike"
    }
]

Output:

[
{
Parent: "cars", 
Key:"com.automotive.cars",
PlanId:3098756839,
"featureKey": "feature1",
"featureDescription": "featureDescription1",
"unitCountLimit": 10,
"amount": 10,
 "productDescription": "Issue for wheels"
},
{
Parent: "cycle", 
Key:"com.automotive.cycle",
PlanId:3098756840,
"featureKey": "feature2”,
"featureDescription": "featureDescription2”,
 "unitCountLimit": 20,
"amount": 20,
"productDescription": "Issue for cycle"
},
{
Parent: "cycle", 
Key:"com.automotive.cycle",
PlanId:3098756840,
"featureKey": "feature3”,
"featureDescription": "featureDescription3”,
 "unitCountLimit": 30,
"amount": 30,
"productDescription": "Issue for cycle"
},
{
Parent: "cycle", 
Key:"com.automotive.cycle",
PlanId:3098756840,
"featureKey": "feature2”,
"featureDescription": "featureDescription4”,
 "unitCountLimit": 40,
"amount": 40,
"productDescription": "Issue for cycle"
}

]

Solution

  • Your input and output don't seem to map correctly ("bike" vs "cycle", etc.). Here's my best guess given what you've provided. One approach is to use nested maps. Use flatMap on the parent instead so you don't need to call flatten later:

    %dw 2.0
    output application/json
    
    var features = payload flatMap (parent) -> do {
      var planId             = parent.annualPricingPlan.pricingPlanId
      var productDescription = parent.annualPricingPlan.productDescription
      ---
      parent.annualPricingPlan.productFeatures map (feature) -> {
        Parent:             parent.parentKey,
        Key:                parent.key,
        PlanId:             planId,
        FeatureKey:         feature.featureKey,
        FeatureDescription: feature.featureDescription,
        unitCountLimit:     feature.unitCountLimit,
        amount:             feature.amount,
        productDescription: productDescription
      }
    }
    ---
    features