Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

How to remove duplicate key from a Json object?

$
0
0

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"  }]

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>