I need to merge JSON objects with same key into one so that it gets an object or array(doesn't matter if result is an object or array) which holds the key and an array for the values
example JSON object:
[{
"meine_frage": "hier kommt die antwort",
"ne_andere_frage": "ne andere antwort",
"was_willst_du": "alles",
"mehr_zur_auswahl": ["einiges", "vieles und", "g\u00e4r nix"]
}, {
"meine_frage": "tom & jerry",
"ne_andere_frage": "mickey maus",
"was_willst_du": "oder",
"mehr_zur_auswahl": ["manches", "einiges", "vieles und", "g\u00e4r nix"]
}, {
"meine_frage": "dick und doof",
"ne_andere_frage": "minnie muas",
"was_willst_du": "nichts",
"mehr_zur_auswahl": ["g\u00e4r nix"]
}]
result should look like:
[{
"meine_frage": ["hier kommt die antwort", "tom & jerry", "dick und doof"],
"ne_andere_frage": ["ne andere antwort", "mickey maus", "minnie muas"],
"was_willst_du": ["alles", "oder"],
"mehr_zur_auswahl": ["einiges", "vieles und", "g\u00e4r nix", "manches", "einiges", "vieles und", "g\u00e4r nix"]
}]
some values are already arrays whereas others not, the keys are genterated dynamically so assume the the key name is unkown
i tried iterating over the key/values with $.each and for loops but without success, also searched the web but couldnt find similar, appreciate any help
JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java. util.
Can JSON have multiple objects? You can pass a single JSON object to create a single element, or a JSON array of group JSON objects to create multiple elements.
You can use reduce()
and concat()
methods for this.
const data = [{"meine_frage":"hier kommt die antwort","ne_andere_frage":"ne andere antwort","was_willst_du":"alles","mehr_zur_auswahl":["einiges","vieles und","gär nix"]},{"meine_frage":"tom & jerry","ne_andere_frage":"mickey maus","was_willst_du":"oder","mehr_zur_auswahl":["manches","einiges","vieles und","gär nix"]},{"meine_frage":"dick und doof","ne_andere_frage":"minnie muas","was_willst_du":"nichts","mehr_zur_auswahl":["gär nix"]}]
const result = data.reduce(function(r, e) {
return Object.keys(e).forEach(function(k) {
if(!r[k]) r[k] = [].concat(e[k])
else r[k] = r[k].concat(e[k])
}), r
}, {})
console.log(result)
You could use reduce
method by passing a callback
function as parameter.
The algorithm is follow: if final object already contains one key then you should initialize the key's value with an empty array. Otherwise, you should append to array value from all items from the array.
let data = [{"meine_frage":"hier kommt die antwort","ne_andere_frage":"ne andere antwort","was_willst_du":"alles","mehr_zur_auswahl":["einiges","vieles und","gär nix"]},{"meine_frage":"tom & jerry","ne_andere_frage":"mickey maus","was_willst_du":"oder","mehr_zur_auswahl":["manches","einiges","vieles und","gär nix"]},{"meine_frage":"dick und doof","ne_andere_frage":"minnie muas","was_willst_du":"nichts","mehr_zur_auswahl":["gär nix"]}]
let result = data.reduce(function(obj, item){
Object.keys(item).forEach(function(key){
if(!obj[key]) obj[key] = [].concat(item[key])
else {
if(Array.isArray(item[key]))
obj[key].push(...item[key])
else
obj[key].push(item[key]);
}
});
return obj;
},{});
console.log([result]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With