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

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

我在产品数组中有一个 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"
}

]

您的输入和输出似乎没有正确映射("bike" 对比 "cycle",等等)。鉴于您提供的内容,这是我的最佳猜测。一种方法是使用嵌套的 maps。在父级上使用 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