I am trying to merge/combine two json objects to get one final json object(merged object). I have tried the fiddle, but I am getting like: [object Object]
if I use $.extend and Uncaught TypeError: testjson1.concat is not a function
error if I use cancat. Please help me how can I get my required json object either using javascript or jquery or angularJS ?
Asked
Active
Viewed 1.2k times
1

Dhana
- 711
- 3
- 14
- 40
-
1Don't alert. Try logging it in console and it should work. Or do `JSON.stringify(finalObject)` to convert the object in string. – void Apr 05 '16 at 08:36
-
first one _is_ want you want – dandavis Apr 05 '16 at 08:36
-
2https://jsfiddle.net/kuunpmx9/2/ – Kartikeya Khosla Apr 05 '16 at 08:37
-
JSON,stringify(obj, null, 2) prints the object even prettier – Nick D Apr 05 '16 at 08:41
-
1The example final result JSON is actually the same as `testjson1`. Can you update the fiddle? – Qwerty Apr 05 '16 at 08:47
-
It's already giving me the correct answer. Use `console.log` for printing instead of `alert`. Also, prefer this `$.extend({}, testjson1, testjson2)` over `$.extend(testjson1, testjson2)`. – kshirish Apr 05 '16 at 09:06
-
@Qwerty, I have updated my fiddle in the question. – Dhana Apr 05 '16 at 09:08
-
@KartikeyaKhosla, I have updated my fiddle in the question as my testjson1 was having some wrong json previously – Dhana Apr 05 '16 at 09:09
-
BTW, you are not really using JSONs. Those are already JavaScript objects. JSON is text only `'{"key":value}'` and can be converted into JavaScript object with `JSON.parse('...')` – Qwerty Apr 05 '16 at 09:10
4 Answers
1
You might want to convert the JSONs into javascript objects, then perform the merging like so and convert back to JSON.
How can I merge properties of two JavaScript objects dynamically?
Basically iterate over the keys and assign them.
var obj1 = JSON.parse(testjson1),
obj2 = JSON.parse(testjson2)
for (var attrname in obj2) { obj1[attrname] = obj2[attrname] }
var finaljsonresult = JSON.stringify(obj1)
But this is just a sneak peak, you might want to see the linked answer for more information.
1
Since this question is tagged with AngularJS, why not convert the JSONs to JavaScript objects using angular.fromJson and then merge them with angular.merge?
Unlike angular.extend, angular.merge recursively descends into object properties of source objects, performing a deep copy.

Cosmin Ababei
- 7,003
- 2
- 20
- 34
0
var obj1 = {
'a': 'aa',
'b': 'bb',
'c': 'cc'
},
obj2 = {
'a': 'aa',
'd': 'dd',
'e': 'ee'
};
for (var key in obj2) {
obj1[key] = obj2[key]
}
var res = JSON.stringify(obj1);
alert(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Qwerty
- 29,062
- 22
- 108
- 136

Hitesh saini
- 1
- 1
-2
The Extend keyword in jquery making stringify:
var object1 = {
apple: 0,
banana: { weight: 52, price: 100 },
cherry: 97
};
var object2 = {
banana: { price: 200 },
durian: 100};
var ss = $.extend(object1, object2);
alert(JSON.stringify(ss));

shivanand s gaddi
- 28
- 5
-
Use appropriate formatting for code while posting comments or answers. – kshirish Apr 05 '16 at 09:07