Search code examples
muledataweavemulesoft

how to map items between two array through dataweave


i have two arrays including shipment and sales . we need to get shipment items and cancel items base on these two arrays. for example shipment arrays , the product 0A6910XL and 01100M was separated shipped 1 pcs. but the externalItemNumber of 01100M is empty.

[
    {
        "sku": "0A6910XL",
        "giQty": "1.000",
        "pickedQty": "1.000",
        "requiredQty": 0,
        "externalItemNumber": "14303087"
    },
    {
        "sku": "01100M",
        "giQty": "1.000",
        "pickedQty": "1.000",
        "requiredQty": 0,
        "externalItemNumber": ""
    }   
]

base on sales order number, we can get the corresponding sales order items like below, we can know the product 01100M be ordered 2 pcs.

[
    {
        "sku": "06910XL",
        "quantity": 1,
        "orderItemId": "14303087"
    },
    {
        "sku": "01100M",
        "quantity": 1,
        "orderItemId": "14303088"
    },
    {
        "sku": "01100M",
        "quantity": 1,
        "orderItemId": "14303089"
    }
]

now, we need to get two arrays , shipment and cancellation. Notes the "01100M" was ordered 2 pcs, but only be shipped 1 pcs.
my expected results is below shipment, we need to get any orderItemId from original sales order to input externalItemNumber . expected output of shipment

[
    {
        "sku": "0A6910XL",
        "giQty": "1.000",
        "pickedQty": "1.000",
        "requiredQty": 0,
        "externalItemNumber": "14303087"
    },
    {
        "sku": "01100M",
        "giQty": "1.000",
        "pickedQty": "1.000",
        "requiredQty": 0,
        "externalItemNumber": "14303088"
    }   
]

expected output of cancellation items

[

    {
        "sku": "01100M",
        "giQty": "1.000",
        "pickedQty": "1.000",
        "requiredQty": 0,
        "externalItemNumber": "14303089"
    }   
]

i can get result through "for each" , but i'd like to know if we can get result through dataweave transformation.

thanks


Solution

  • You can use join() or one of its variants from the Arrays module to join the Arrays, then map as needed.

    You can find examples by searching previous join answers here with tag [dataweave]: [dataweave] join.