1

Hi I need to transform the following JSON object:

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

I am trying to transform this with each attribute having an array of values where their value is the codeValue and it's an array of those string values.

This is the desired output:

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

This is the Dataweave. I cannot determine how to get the attribute names (i.e. color, chemicals as keys with the desired data.

There is not a lot of good examples on transforming data with Dataweave and I have spent a lot of time trying to figure this out.

This is one of the dataweaves that got there somewhat, but isn't the solution:

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

Your help is greatly appreciated.

jerney
  • 2,187
  • 1
  • 19
  • 31
BArTMAN
  • 49
  • 6

1 Answers1

1

You can do something like this:

%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)
}
jerney
  • 2,187
  • 1
  • 19
  • 31