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

6
  • 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. Commented Jul 24, 2017 at 8:37
  • @t.niese SIr, result Im getting from this $.extend(true, {}, x, y); is not merging, its printing only first variable. Commented Jul 24, 2017 at 8: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. Commented Jul 24, 2017 at 8:43
  • @chsdk Yes valid point but how do i merge these two, any other way to get result Commented Jul 24, 2017 at 8:45
  • Do this work obj_1.Template.Section = $.merge( obj_1.Template.Section, obj_2.Template.Section ) Commented Jul 24, 2017 at 8:47

3 Answers 3

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; }

Sign up to request clarification or add additional context in comments.

3 Comments

This is working fine.. Can't we keep null values in the array..?? instead of removing them
@MallikarjunHampannavar That's what I was talking about, this is a nice implementation ;)
actually you get more null values in the merged array than you have in the sources arrays. you might fill it up to the wanted count ...
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;

Comments

0

Try this

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.