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.