'How to remove object from array JSON with filter DataWeave

I have this output

    {
  "array": [
    {
      "value": "abc",
      "Product2": {
        "Name": "phone",
        "id": "123abc"
      },
      "products": [
        {
          "id": "123abc",
          "value": "PKabc"
        }
      ]
    }
  ]
}

From this I need to remove entire object from "products" array if value and id (in the array ) is the same in the "products" array

I'm trying in this way but not work

(payload.array filter ($.value == $.value))

Any suggestions?

Thanks



Solution 1:[1]

You need to iterate over the payload.array array and then filter by the value attribute over the products array.

%dw 2.0
output application/json
---
payload.array map ((arrayItem, arrayIndex) ->
    arrayItem.products filter ($.id != arrayItem.value) 
)

Solution 2:[2]

Please check this

%dw 2.0 output application/json

payload.array map if(($.products map $.id) contains $.Product2.id)($ - "products") else $

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 HariKrishna Juttuka