Search code examples
muledataweavemulesoftanypoint-studiomule4

Dataweave filtering nested arrays and displaying in descending order


I'm trying to filter an array based on some values nested in objects.

My data pertains to offers (array), customers(array) with tickets(array) and other child arrays. I want to orderBy to get all the customers information ordered by the latest timeStamp (attribute in tickets array)

From the example, offer 1 has customer 50001 with tickets 1001, 1002 and customer 50002 with tickets 1003, 1004. I want the customer which has latest timestamp in all the tickets available to be displayed first: (Desc order) with all the other passengers ordered accordingly.

Request Payload:

{
    "count": 1,
    "offers": [{
        "offerInfo": {
            "orderNumber": "1",
            "orderCreationDtTime": "2023-01-10 00:00:00"
        },
        "customers": [{
                "customerInfo": {
                    "name": {
                        "frstNm": "JOHN",
                        "lstNm": "DOE"
                    }
                },
                "customerNum": "50001",
                "tickets": [{
                        "timestamp": "2023-01-07 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1001",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    },
                    {
                        "timestamp": "2023-01-11 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1002",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "3",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "4",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    }
                ]
            },
            {
                "customerInfo": {
                    "name": {
                        "frstNm": "FAN",
                        "lstNm": "SING"
                    }
                },
                "customerNum": "50002",
                "tickets": [{
                        "timestamp": "2023-01-10 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1003",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    },
                    {
                        "timestamp": "2023-01-19 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1004",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    }
                ]
            }
        ]
    }]
}

Expecting Payload after transform message:

{
    "count": 1,
    "offers": [{
        "offerInfo": {
            "orderNumber": "1",
            "orderCreationDtTime": "2023-01-10 00:00:00"
        },
        "customers": [{
                "customerInfo": {
                    "name": {
                        "frstNm": "FAN",
                        "lstNm": "SING"
                    }
                },
                "customerNum": "50002",
                "tickets": [{
                        "timestamp": "2023-01-19 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1004",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    },
                    {
                        "timestamp": "2023-01-10 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1003",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    }
                ]
            },
            {
                "customerInfo": {
                    "name": {
                        "frstNm": "JOHN",
                        "lstNm": "DOE"
                    }
                },
                "customerNum": "50001",
                "tickets": [{
                        "timestamp": "2023-01-11 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1002",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "3",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "4",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    },
                    {
                        "timestamp": "2023-01-07 00:38:00.167000",
                        "ticketService": {
                            "ticketNum": "1001",
                            "ticketType": "3"
                        },
                        "ticketReps": [{
                                "seq": "1",
                                "comment": "1st",
                                "location": "US"
                            },
                            {
                                "seq": "2",
                                "comment": "2nd",
                                "location": "US"
                            }
                        ]
                    }
                ]
            }
        ]
    }]
}

Solution

  • Below script will help you.

    %dw 2.0
    output application/json
    import * from dw::util::Values
    ---
     payload update ["offers","customers"] with (
        (($ map (
            $  update {
                    case .tickets ->  ($ orderBy $.timestamp  as LocalDateTime {format : "yyyy-MM-dd HH:mm:ss.SSSSSS"}) [-1 to 0]
            }
        )) orderBy $.tickets[0].timestamp as LocalDateTime {format : "yyyy-MM-dd HH:mm:ss.SSSSSS"}) [-1 to 0]
     )