Xullibeut Xallibeut
Xullibeut Xallibeut

Reputation: 15

Dataweave 2 transforming an array of objects having a id field, into a object whose elements are maps

need some suggestion for a dataweave 2 transformation that can transform the following input (Array of Object having an id field) to the following output (grouping in an array the objects sharing the same id into a map, having the id as key). Input :

[
  {
    "id": "1117",
    "Tot": "10.0",
    "Per": "7/2025"
  },
  {
    "id": "1117",
    "Tot": "200.0",
    "Per": "2/2021"
  },
  {
    "id": "7997",
    "Tot": "78.0",
    "Per": "10/2023"
  }
]

output

{
 "1117": [
  {
   "id": "1117",
   "Tot": "10.0",
   "Per": "7/2025"
  },
  {
   "id": "1117",
   "Tot": "200.0",
   "Per": "2/2021"
  }
 ],
 "7997": [
  {
   "id": "7997",
   "Tot": "78.0",
   "Per": "10/2023"
  }
 ]
}

Any idea?

Thanks

Upvotes: 0

Views: 298

Answers (2)

Michael Jones
Michael Jones

Reputation: 1910

%dw 2.0
output application/json
---
payload groupBy $.id

You just want to group by the ID? There ya go.

I would recommend going to https://developer.mulesoft.com/learn/dataweave and then going to the tutorial tab (top right). It will walk through these basic scenarios :)

Upvotes: 3

Janardan Kelkar
Janardan Kelkar

Reputation: 170

payload groupBy $.id

Adding more chars to reach 30.

Upvotes: -1

Related Questions