[Solved] trouble merging two json strings


Here’s how I would do it without the use of the deep extend module.

First rearrange JSON1 so that the name property of each object becomes an object key:

var obj1 = Object.keys(obj).reduce(function (p, c) {
  var o = obj[c], name = o.name;
  p[o.name] = JSON.parse(JSON.stringify(o));
  return p;
}, {});

OUTPUT

{ Car: {...} }

Then, loop over the array of objects. If there is an object key that matches the name property overwrite the properties from JSON1 to JSON2 if they exist.

for (var i = 0, l = arr1.length; i < l; i++) {
  var el = arr1[i], key = el.name;
  if (obj1[key]) {
    for (var p in el) {
     if (obj1[key][p]) el[p] = obj1[key][p];
    }
  }
}

DEMO

solved trouble merging two json strings