1

I have to merge two json object and make it one json object, My json objects are like below

var obj_1 = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [{
      "Id": "Section_1",
      "Name": "Task 1: Planning and co-ordination",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_2",
      "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null]
  }
}

var obj_2 = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [null, null, {
      "Id": "Section_3",
      "Name": "Task 2: Perform fitting operation as per the drawing",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_4",
      "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null]
  }
}

How do I merge like below variable

var mergedObj = {
  "?xml": {"version": "1.0", "encoding": "utf-8"},
  "Template": {
    "Name": "Capital Goods-Tool and Die Maker L5 Set1",
    "Section": [{
      "Id": "Section_1",
      "Name": "Task 1: Planning and co-ordination",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_2",
      "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_3",
      "Name": "Task 2: Perform fitting operation as per the drawing",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, {
      "Id": "Section_4",
      "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines",
      "Description": "",
      "Value": "",
      "NoofQuestions": "0",
      "IsSectionQuestionsMandatory": "false"
    }, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
  }
};

I have tried below code

var merged = {};
Object.assign(merged, result, resultresult);

and also I have tried this also How to merge two object values by keys

but nothing is working for me.. Please anybody help me to resolve this

Vladimir G.
  • 885
  • 7
  • 15
  • 2
    What does not work for your, what is the result you get and how does it differ to the one that you expect. Beside that, those are JavaScript Objects, not JSON objects. JSON in JavaScript is a stringified version of data. – t.niese Jul 24 '17 at 08:37
  • @t.niese SIr, result Im getting from this $.extend(true, {}, x, y); is not merging, its printing only first variable. – Mallikarjun Hampannavar Jul 24 '17 at 08:41
  • @MallikarjunHampannavar that's because both objects have the same properties, so only one property can be taken, an object can't have a duplicate property. – cнŝdk Jul 24 '17 at 08:43
  • @chsdk Yes valid point but how do i merge these two, any other way to get result – Mallikarjun Hampannavar Jul 24 '17 at 08:45
  • Do this work obj_1.Template.Section = $.merge( obj_1.Template.Section, obj_2.Template.Section ) – gjijo Jul 24 '17 at 08:47
  • @MallikarjunHampannavar You need to implement a solution that suits your case. – cнŝdk Jul 24 '17 at 08:50

3 Answers3

1

You could merge all truthy values with a recursive approach.

function merge(source, target) {
    Object.keys(source).forEach(function (key) {
        if (!source[key]) {
            return;
        }
        if (typeof source[key] === 'object') {
            target[key] = target[key] || (Array.isArray(source[key]) ? [] : {});
            return merge(source[key], target[key]);
        }
        target[key] = source[key];
    });
}
var obj_1 = { "?xml": { "version": "1.0", "encoding": "utf-8" }, "Template": { "Name": "Capital Goods-Tool and Die Maker L5 Set1", "Section": [{ "Id": "Section_1", "Name": "Task 1: Planning and co-ordination", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, { "Id": "Section_2", "Name": "NOS 1: CSC/N0307 Plan and co-ordinate the making of tools and die", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, null, null] } },
    obj_2 = { "?xml": { "version": "1.0", "encoding": "utf-8" }, "Template": { "Name": "Capital Goods-Tool and Die Maker L5 Set1", "Section": [null, null, { "Id": "Section_3", "Name": "Task 2: Perform fitting operation as per the drawing", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, { "Id": "Section_4", "Name": "NOS 2: CSC/N0308 Perform fitting operations on metal components using hand tools and manually operated machines", "Description": "", "Value": "", "NoofQuestions": "0", "IsSectionQuestionsMandatory": "false" }, null, null] } },
    merged = {};

merge(obj_1, merged);
merge(obj_2, merged);

console.log(merged);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Try this,

obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);

obj_1 will hold the merged result of section array from obj_1 and obj_2;

Manu Benjamin
  • 987
  • 9
  • 24
0

Try this

obj_1.Template.Section = obj_1.Template.Section.concat(obj_2.Template.Section);
obj_1.Template.Section.sort();
Ananth Cool
  • 125
  • 8