如何将对象数组转换为字符串数组而不丢失 Dataweave 中的密钥?

How can I transform an array of objects to an array of strings and not lose the key in Dataweave?

您好,我需要转换以下 JSON 对象:

{
  "products": [
    {
      "itemno": "123131",
      "description" : "Big Widget",
      "attributes": [
        {
          "color": [
            {
              "value": "Red",
              "codeValue": "RED_NO2"
            },
            {
              "value": "Blue Licorice",
              "codeValue": "BLUE-355"
            }
          ]
        },
        {
          "chemicals": [
            {
              "value": "Red Phosphorous",
              "codeValue": "RED_PHOS"
            },
            {
              "value": "Chlorine Bleach",
              "codeValue": "CHLRN_BLCH"
            }
          ]
        }
      ]
    }
  ]
}

我正在尝试将其转换为具有值数组的每个属性,其中它们的值是 codeValue,它是这些字符串值的数组。

这是期望的输出:

{
  "products": [
    {
      "itemno": "123131",
      "description: : "Big Widget",
      "attributes": [
        {
          "color": ["RED_NO2", "BLUE-355"]
        },
        {
          "chemicals": ["RED_PHOS", "CHLRN_BLCH"]
        }
      ]
    }
  ]
}

这是Dataweave。我无法确定如何获取属性名称(即颜色、化学品作为具有所需数据的键。

没有很多关于使用 Dataweave 转换数据的好例子,我花了很多时间试图弄清楚这一点。

这是某种程度上达到目的的数据编织之一,但这不是解决方案:

%dw 1.0
%output application/json
---
payload.products map 
{
    "ItemNo" : $.sku,
    "Desc" : $.description,
    "Test" : "Value",
    "Attributes" : $.attributes map
    {
        '$$' : $ pluck $.value
    }
}

非常感谢您的帮助。

你可以这样做:

%dw 1.0
%output application/json

%function attributeCodeValues(attributes)
  attributes map ((attr) -> 
    attr mapObject ((values, descriptor) ->
      {
        (descriptor): values map $.codeValue
      }
    )
  )
---
payload.products map {
    "ItemNo" : $.sku,
    "Desc" : $.description,
    "Test" : "Value",
    "Attributes" : attributeCodeValues($.attributes)
}