Sudhan Ramachandran
Sudhan Ramachandran

Reputation: 3

Mule Dataweave - converting array of array into Array of object

It would be helpful if someone could help me on below dataweave expression

[ { "id": "1001", "email": null,
"role": "admin", "updatedAt": "2019-12-04T22:39:00.322000Z", "Attributes": [ { "id": "1001A", "name": "GEO", "value": null }, { "id": "1001B", "name": "Group", "value": "admin" }, { "id": "1001C", "name": "Title", "value": null }, { "id": "1001D", "name": "Location", "value": "New York" } ], "isActive": false }, { "id": "2001", "email": null,
"role": "student", "updatedAt": "2019-12-05T22:39:00.322000Z", "Attributes": [ { "id": "2001A", "name": "GEO", "value": null }, { "id": "2001B", "name": "Group", "value": "admin" }, { "id": "2001C", "name": "Title", "value": null }, { "id": "2001D", "name": "Location", "value": "New York" } ], "isActive": false }, { "id": "3001", "email": null,
"role": "admin", "updatedAt": "2019-12-04T22:39:00.322000Z", "Attributes": [ { "id": "3001A", "name": "GEO", "value": null }, { "id": "3001B", "name": "Group", "value": "admin" }, { "id": "3001C", "name": "Title", "value": null }, { "id": "3001D", "name": "Location", "value": "New York" } ], "isActive": false } ]

And My Output should be like below

[ { "id": "1001", "email": null,
"role": "admin", "updatedAt": "2019-12-04T22:39:00.322000Z", "GEO" : null, "Group":"admin", "Title":null, "Location":"New York", "isActive": false

},
{
    "id": "2001",
    "email": null,            
    "role": "student",
    "updatedAt": "2019-12-05T22:39:00.322000Z", 
    "GEO" : null,
    "Group":"student",
    "Title":null,
    "Location":"New York",
    "isActive": false
},
{
    "id": "3001",
    "email": null,            
    "role": "admin",
    "updatedAt": "2019-12-04T22:39:00.322000Z",
    "GEO" : null,
    "Group":"admin",
    "Title":null,
    "Location":"New York",
    "isActive": false
}

]

Upvotes: 0

Views: 201

Answers (1)

olamiral
olamiral

Reputation: 1296

You can use the following DataWeave expression:

%dw 2.0
output application/json
fun toObject(attributes) = ((attributes map ($.name): $.value) reduce ($ ++ $$))
---
payload map (($ - "Attributes") ++ toObject($.Attributes))

Using the provided payload as input, the output payload is:

[
  {
    "id": "1001",
    "email": null,
    "role": "admin",
    "updatedAt": "2019-12-04T22:39:00.322000Z",
    "isActive": false,
    "Location": "New York",
    "Title": null,
    "Group": "admin",
    "GEO": null
  },
  {
    "id": "2001",
    "email": null,
    "role": "student",
    "updatedAt": "2019-12-05T22:39:00.322000Z",
    "isActive": false,
    "Location": "New York",
    "Title": null,
    "Group": "admin",
    "GEO": null
  },
  {
    "id": "3001",
    "email": null,
    "role": "admin",
    "updatedAt": "2019-12-04T22:39:00.322000Z",
    "isActive": false,
    "Location": "New York",
    "Title": null,
    "Group": "admin",
    "GEO": null
  }
]

Upvotes: 1

Related Questions