Dataweave 2.0:将嵌套数组转换为带有父级详细信息的平面列表

时间:2019-04-19 23:39:55

标签: dataweave mulesoft

我在一系列产品中有productFeatures的列表。
我需要为productFeatures数组中的每个功能创建产品列表。

我目前正在使用Dataweave 2.0实现此转换。

试图使用map() paylaod.pricingPlan.productFeatures,但它返回了productFeatures的完整列表。我需要每个列表的父母详细信息。

输入:

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

输出:

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

]

1 个答案:

答案 0 :(得分:2)

您的输入和输出似乎映射不正确(“自行车”与“自行车”等)。根据您提供的内容,这是我的最佳猜测。一种方法是使用嵌套的map。请改为在父母上使用flatMap,这样您以后就不必再致电flatten

%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