2

I feel like I am missing something very obvious... I can not get JSON objects to merge and hold their sub objects. I have four JSON objects that I need to merge.

Customer: {"datecreated":"12/10/2015 9:18 am","id":"5566447","entityid":"652967804","entitystatus":"13","companyname":"ABCTools Inc parent","altname":"ABCTools Inc parent","email":"[email protected]","phone":"(512) 123-4567","subsidiary":"1"}

Contact: {"contact":{"datecreated":"12/10/2015 9:18 am","id":"5566544","entityid":"JohnDoe Customer","firstname":"JohnDoe","lastname":"Contact","email":"[email protected]","phone":"(512) 123-4567","company":"5566447","subsidiary":"1"}}

SubCustomer: {"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"[email protected]","phone":"(512) 123-4567","subsidiary":"1"}

subContact: {"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"[email protected]","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

I can merge them with out issue. But, they do not retain their structure. Below is the function I was originally using to merge them:

function concatObjects(cust,contact){
  for(var key in contact){
    cust[key]=contact[key];
  }
  return cust;
}

This works great on the Customer and Contact:

var mergedCust=concatObjects(customer,contact);

mergedCust: {"datecreated":"12/10/2015 9:18 am","id":"5566447","entityid":"652967804","entitystatus":"13","companyname":"ABCTools Inc parent","altname":"ABCTools Inc parent","email":"[email protected]","phone":"(512) 123-4567","subsidiary":"1","contact":{"datecreated":"12/10/2015 9:18 am","id":"5566544","entityid":"JohnDoe Customer","firstname":"JohnDoe","lastname":"Contact","email":"[email protected]","phone":"(512) 123-4567","company":"5566447","subsidiary":"1"}}

But, when I try it on the Subcustomer and sub contact they are simply aprended to each other, like this:

mergedSubCust: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"[email protected]","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"[email protected]","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

I can see that this is because they are sub-object. So I tried this:

function concatObjects(cust,contact){
    for(var key in contact){
      if(typeof contact[key]=='object'){
        cust[key]={};
        for(var subKey in contact){
          cust[key]=contact[subKey];
        }
      }else{
        cust[key]=contact[key];
      }
    }
    return cust;
  }

But, it still returns the same:

mergedSub: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"[email protected]","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"[email protected]","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}

Below is how they should look when all merged together:

{
  "datecreated": "12\/10\/2015 9:18 am",
  "id": "5566447",
  "entityid": "652967804",
  "entitystatus": "13",
  "companyname": "ABCTools Inc parent",
  "altname": "ABCTools Inc parent",
  "email": "[email protected]",
  "phone": "(512) 123-4567",
  "subsidiary": "1",
  "contact": {
    "datecreated": "12\/10\/2015 9:18 am",
    "id": "5566544",
    "entityid": "JohnDoe Customer",
    "firstname": "JohnDoe",
    "lastname": "Contact",
    "email": "[email protected]",
    "phone": "(512) 123-4567",
    "company": "5566447",
    "subsidiary": "1"
  },
  "subcustomer": {
    "datecreated": "12\/10\/2015 9:18 am",
    "id": "5566448",
    "entityid": "652967805",
    "entitystatus": "13",
    "companyname": "ABCTools Inc sub",
    "altname": "ABCTools Inc sub",
    "email": "[email protected]",
    "phone": "(512) 123-4567",
    "subsidiary": "1",
    "subcontact": {
      "datecreated": "12\/10\/2015 7:14 am",
      "id": "5566142",
      "entityid": "JohnDoe SubCustomer",
      "firstname": "Johndoe",
      "lastname": "Subcustomer",
      "email": "[email protected]",
      "phone": "(512) 123-4567",
      "company": null,
      "subsidiary": "1"
    }
  }
}

I believe what I'm missing is how to just take that whole second object and place it inside of the first object, no matter what datatype is is.

I've Googled around a lot, but and not finding anything specific to this issue... Thanks for any insight into this.

2
  • finally how should they look like? Commented Dec 10, 2015 at 17:12
  • @gurvinder372 I updated the question to add that. Thanks. Commented Dec 10, 2015 at 17:24

1 Answer 1

1

Assuming that they are part of one json right now as

var oneObj = {
   customer: {},
   contact: {},
   subcustomer: {},
   subcontact: {}
};

create another object as

var finalObj = {};
finalObj.customer = oneObj.customer;
finalObj.customer.contact = oneObj.contact;
finalObj.customer.subcustomer = oneObj.subcustomer;
finalObj.customer.subcustomer.subcontact = oneObj.subcontact;
Sign up to request clarification or add additional context in comments.

2 Comments

Wow... Thanks... Figured I was missing the obvious. Though, I did not think it was that obvious. lol. Thanks!
@W3BGUY Happy to help. Thanks you for the quick upvote and accept

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.