Merge two JSON objects

I have two JSON objects with the same structure and I want to combine them together using Javascript. Is there an easy way to do this?

+64
json javascript
Jan 11 '09 at 20:29
source share
13 answers

Based on your description in the comments, you simply execute the concat array:

var jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}]; var jsonArray2 = [{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}]; jsonArray1 = jsonArray1.concat(jsonArray2); // jsonArray1 = [{'name': "doug", 'id':5}, {'name': "dofug", 'id':23}, //{'name': "goud", 'id':1}, {'name': "doaaug", 'id':52}]; 
+83
Jan 12 '09 at 0:07
source share

If you want to copy properties:

 var json1 = { value1: '1', value2: '2' }; var json2 = { value2: '4', value3: '3' }; function jsonConcat(o1, o2) { for (var key in o2) { o1[key] = o2[key]; } return o1; } var output = {}; output = jsonConcat(output, json1); output = jsonConcat(output, json2); 

The output of the above code: { value1: '1', value2: '4', value3: '3' }

+34
Jan 11 '09 at 20:50
source share

You can use the jquery extend method.

Example:

 o1 = {"foo":"bar", "data":{"id":"1"}}; o2 = {"x":"y"}; sum = $.extend(o1, o2); 

Result:

 sum = {"foo":"bar", "data":{"id":"1"}, "x":"y"} 
+21
Mar 24 '15 at 16:17
source share

The actual way is to use JS Object.assign.

 Object.assign(target, ...sources) 

MDN Link

There is another object distribution operator that is offered for ES7 and can be used with Babel plugins.

  Obj = {...sourceObj1, ...sourceObj2} 
+16
Jun 24 '17 at 8:26
source share

One solution is to use a list / array:

 var first_json = {"name":"joe", "age":27}; var second_json = {"name":"james", "age":32}; var jsons = new Array(); jsons.push(first_json); jsons.push(second_json); 

Result

 jsons = [ {"name":"joe", "age":27}, {"name":"james", "age":32} ] 
+14
Jan 11 '09 at 20:40
source share

You can use the Object.assign () method . The Object.assign () method is used to copy the values โ€‹โ€‹of all enumerated own properties from one or more source objects to the target object. It will return the target. [one]

 var o1 = { a: 1 }, o2 = { b: 2 }, o3 = { c: 3 }; var obj = Object.assign(o1, o2, o3); console.log(obj); // { a: 1, b: 2, c: 3 } 
+3
Jul 03 '18 at 18:00
source share

I use

let x = {a: 1, b: 2, c: 3}

let y = {c: 4, d: 5, e: 6}

let z = Object.assign (x, y)

console.log (g)

// OUTPUTS:

{a: 1, b: 2, c: 4, d: 5, e: 6}

https://www.quora.com/How-can-I-add-two-JSON-objects-into-one-object-JavaScript

+2
Sep 10 '18 at 23:16
source share

In order, you can do this in one line of code. you will need json2.js to do this (maybe you already have one). the two json objects here are inseparable strings.

 json1 = '[{"foo":"bar"},{"bar":"foo"},{"name":"craig"}]'; json2 = '[{"foo":"baz"},{"bar":"fob"},{"name":"george"}]'; concattedjson = JSON.stringify(JSON.parse(json1).concat(JSON.parse(json2))); 
+1
Jan 11 '09 at 21:24
source share

Just try this using the underscore

 var json1 = [{ value1: '1', value2: '2' },{ value1: '3', value2: '4' }]; var json2 = [{ value3: 'a', value4: 'b' },{ value3: 'c', value4: 'd' }]; var resultArray = []; json1.forEach(function(obj, index){ resultArray.push(_.extend(obj, json2[index])); }); console.log("Result Array", resultArray); 

Result

+1
Dec 08 '15 at 12:51 on
source share
 var baseArrayOfJsonObjects = [{},{}]; for (var i=0; i<arrayOfJsonObjectsFromAjax.length; i++) { baseArrayOfJsonObjects.push(arrayOfJsonObjectsFromAjax[i]); } 
0
Jan 12 '09 at 5:05
source share

Javascript only =)

 var objA = { 'a': 1, 'b': 2} var objB = { 'x': 3, 'y': 5} objA.nameNewObj = objB 

enter image description here

0
Oct 27 '17 at 12:15
source share

I use:

 let jsonFile = {}; let schemaJson = {}; schemaJson["properties"] = {}; schemaJson["properties"]["key"] = "value"; jsonFile.concat(schemaJson); 
0
Nov 11 '18 at 3:21
source share

if you use TypeScript, you can use the distribution operator ( ... )

 var json = {...json1,...json2} 
0
Jun 14 '19 at 7:55
source share



All Articles