0
votes

Can someone help me in writing the dataweave in mule 4 for

CSV input

name, dateOfSelected, joiningDate, leavingDate, age
John, 2020-02-20, 2020-02-22, 2020-04-30, 28
Sam, 2020-02-21, 2020-02-22, 2020-04-20, 30
Ben, 2020-03-03, 2020-03-04, 2020-04-29, 34
Mike, 2020-04-21, 2020-04-21, 2020-06-19, 25

expected JSON output should contain name, joiningDate and leavingDate grouped under dateOfSelected field value

{
  "failedToQuery": "No",
  "result": "success",
  "dataAvailable" : "yes"
  "2020-02-20": {
    "joiningDate": "2020-02-22",
    "leavingDate": "2020-04-30",
    "name": "John"
  },
  "2020-02-21": {
    "joiningDate": "2020-02-22",
    "leavingDate": "2020-04-20",
    "name": "Sam"
  },
  "2020-03-03": {
    "joiningDate": "2020-03-04",
    "leavingDate": "2020-04-29",
    "name": "Ben"
  },
  "2020-04-21": {
    "joiningDate": "2020-04-21",
    "leavingDate": "2020-06-19",
    "name": "Mike"
  }
}

I will be running a db query to fetch the CSV input data using dateOfSelected field value. In case no data returned from DB the output should look be

{
      "failedToQuery": "No",
      "result": "Success",
      "dataAvailable" : "no"
}
1

1 Answers

1
votes

You can use this script:

%dw 2.0
output application/json
---
{failedToQuery: "No"} ++
{result:"success"} ++
{dataAvailable: if (isEmpty(payload)) "no" else "yes"} ++
(payload 
    map $ - "age"   // remove age attribute
    groupBy (item, index) -> item.dateOfSelected
)