Search code examples
mulemule-studiomule-componentdataweavemule-esb

Extract Max/earliest date from JSON message in dataweave 2.0


My message structure:

{
    "empid": "abc",
    "homeCountry": "IND",
    "dateOfBirth": "1969-01-01",
    "personalInformation": [
        {
            "salutation": "Mr",
            "firstName": "Ram",
            "lastName": "Naresh"
        }
    ],
    "EmpInfo": [
        {
            "hireDate": "2000-01-01",
            "LevDate": "2018-07-25",
            "jobInformation": [
                {
                    "isFullTimeEmployee": true,
                    "jobTitle": "Engineer",
                    "effectiveStartDate": "2018-01-05"
                },
                {
                    "isFullTimeEmployee": true,
                    "jobTitle": "Store Manager",
                    "effectiveStartDate": "2019-01-05"
                }
            ]
        }
    ]
}

I would like to extract the max date and index of the same so that i can declare it as var and use it in the mapping.


Solution

  • Your question is not clear enough. What date field do you want to use? Here I created a solution that returns the index and the value of the jobInformation with the oldest effectiveStartDate

    payload.EmpInfo[0].jobInformation 
        map ((item, index) -> {value: item, index: index}) 
        maxBy ((item) -> item.value.effectiveStartDate as Date)