0
votes

I'm just getting started with Dataweave and trying to figure out how to transform data from this particular JSON response. I'm stumped after fairly exhaustively reading documentation and searching for examples. Can't find anything quite like it. Below is the payload I'm working with:

[
  {
    "columnMetadata": [
      {
        "name": "shape",
        "columnIndex": 0,
        "dataType": "string",
        "schemaType": "Static"
      },
      {
        "name": "color",
        "columnIndex": 1,
        "dataType": "string",
        "schemaType": "Static"
      }
    ],
    "rowData": [
      [
        "square",
        "yellow"
      ],
      [
        "circle",
        "green"
      ],
      [
        "star",
        "blue"
      ]
    ]
  }
]

The transformation I'm trying to achieve is as such:

[
    {
        "shape": "square",
        "color": "yellow"
    },
    {
        "shape": "circle",
        "color": "green"
    },
    {
        "shape": "star",
        "color": "blue"
    }
]

Any help much appreciated!

3

3 Answers

5
votes

The way to resolve this problem is by using dynamic objects This feature allows to dynamically compose an object from other objects or array the objects in this case. It is similar to the spread operator in js.

%dw 2.0
output application/json
---
payload flatMap ((item, index) -> do {
    var metadataNames =  item.columnMetadata map ((metadata, index) ->  metadata.name)
    ---
    item.rowData map ((datas, index) -> 
        {
            (
                datas map ((data, index) -> 
                    {
                        (metadataNames[index]):data
                    }
                )
            )
        }
    )
})
1
votes

This transform should work to get your output

payload..rowData flatMap (v) -> (v map ({shape: $[0], color: $[1]}))
0
votes

If my understanding is correct, you wanted to pick data from rowData following the information found in columnMetadata (specific information will be picked based on columnIndex and key will be based on name i.e. shape should come from rowData[0] as columnMetadata.columnIndex is 0 and columnMetada.name is shape). This will mean that whatever information in columnMetadata will impact how you read the rowData.

You can attain this using combination of map and reduce. Reduce to iterate values of your columnMetadata and accumulate the result, and map to perform reduce for each member of your rowData.

See below dataweave:

%dw 2.0
output application/json

---
using (columnMetadata = flatten(payload.columnMetadata))
flatten(payload.rowData) map (row) -> columnMetadata reduce ((item, acc={}) -> acc ++ {(item.name): row[item.columnIndex]})

This will result to:

[
  {
    "shape": "square",
    "color": "yellow"
  },
  {
    "shape": "circle",
    "color": "green"
  },
  {
    "shape": "star",
    "color": "blue"
  }
]