Search code examples
muledataweavemulesoftmule4mule-esb

How to filter the sku empty and remove all the coincidense that the same order number


There is an array of parent elements that you need filter and remove if the sky are empty and get the number and remove all the coincidences of the principal array.

Input:

[{
    "number": "7358",
    "sku": "301-01"
}, {
    "number": "7358",
    "sku": "301-02"
}, {
    "number": "7359",
    "sku": ""
}, {
    "number": "7359",
    "sku": "301-04"
}, {
    "number": "7356",
    "sku": ""
}, {
    "number": "7356",
    "sku": "301-05"
}, {
    "number": "7356",
    "sku": "301-07"
}]

Output:

[{
    "number": "7358",
    "sku": "301-01"
}, {
    "number": "7358",
    "sku": "301-02"
}]

In Output we only find the elements that complied with having their sku with content.


Solution

  • Try with this script:

    The idea is to collect all number(s) where the sku is "" and create an array out of these. After that you can iterate through the payload and filter out objects where the number in the object is present in the array created in the previous step.

    %dw 2.0
    output application/json
    var atleastOneEmptySku = (payload filter ($.sku == ""))..number
    ---
    payload filter (!(atleastOneEmptySku contains $.number))