Dataweave to remove json elements from values after groupBy | Mule 3.9

365 views Asked by At

I am trying to transform an xml based on groupBy in dataweave, however I also need to remove few json attributes from output.

Input json:

[   {
    "m": {
      "a": "a",
      "b": "b"
    },
    "tag1": "A",
    "tag2": "v1",
    "tag3": "v1"   },   {
    "m": {
      "a": "a",
      "b": "b"
    },
    "tag1": "A",
    "tag2": "v2",
    "tag3": "v2"   },   {
    "m": {
      "a": "a",
      "b": "b"
    },
    "tag1": "C",
    "tag2": "v3",
    "tag3": "v3"   } ]

Output json

 **{
  "A": [
    {
      "tag2": "v1",
      "tag3": "v1"
    },
    {
      "tag2": "v2",
      "tag3": "v2"
    }
  ],
  "C": {
    "tag2": "v3",
    "tag3": "v3"
  }
}**

I have tried following transformation (Mule 3.9) however the could not remove the extra attributes in json.

payload groupBy (item) -> item.tag1

Appreciate any suggestion on this and possibly explain how this can be achieved.

1

There are 1 answers

3
machaval On

The way to iterate over an object is using mapObject and then you can filter the objects to remove the elements that are not wanted

{a: [1,2,3], b: [2,3]} mapObject ((value,key) -> 
    {
       (key): value filter ((value, index) -> value > 2)
    }
 )

This will output

{
  "a": [
    3
  ],
  "b": [
    3
  ]
}