I need to merge two arrays of objects based on a common field, keeping the objects that are present in both arrays and combining their fields.
I am getting duplicate keys after merging two arrays. How to remove duplicate keys in JSON?
DW Script
%dw 2.0import * from dw::core::Arraysoutput application/jsonvar array1 = [ { "id": 1, "name": "John" }, { "id": 2, "name": "Jane" }]var array2 = [ { "id": 1, "age": 30 }, { "id": 3, "age": 25 }, { "id": 2, "age": 25 }]var mergeArray = array1 map((item) -> array2 filter((item2, index) -> (item2.id == item.id)) map (item2) -> (item2 ++ item))---flatten(mergeArray)
Output - Getting duplicate id
[ {"id": 1,"age": 30,"id": 1,"name": "John" }, {"id": 2,"age": 25,"id": 2,"name": "Jane" }]