title: Merging JSON objects with JSON sub-objects
body: <p>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.</p>
  
  <pre><code>Customer: {"datecreated":"12/10/2015 9:18 am","id":"5566447","entityid":"652967804","entitystatus":"13","companyname":"ABCTools Inc parent","altname":"ABCTools Inc parent","email":"jdoe@email.com","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":"jdoe@email.com","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":"jdoe@email.com","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":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>function concatObjects(cust,contact){
    for(var key in contact){
      cust[key]=contact[key];
    }
    return cust;
  }
  </code></pre>
  
  <p>This works great on the Customer and Contact:</p>
  
  <pre><code>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":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1","contact":{"datecreated":"12/10/2015 9:18 am","id":"5566544","entityid":"JohnDoe Customer","firstname":"JohnDoe","lastname":"Contact","email":"jdoe@email.com","phone":"(512) 123-4567","company":"5566447","subsidiary":"1"}}
  </code></pre>
  
  <p>But, when I try it on the Subcustomer and sub contact they are simply aprended to each other, like this:</p>
  
  <pre><code>mergedSubCust: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}
  </code></pre>
  
  <p>I can see that this is because they are sub-object.  So I tried this:</p>
  
  <pre><code>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;
    }
  </code></pre>
  
  <p>But, it still returns the same:</p>
  
  <pre><code>mergedSub: {"subcustomer":{"datecreated":"12/10/2015 9:18 am","id":"5566448","entityid":"652967805","entitystatus":"13","companyname":"ABCTools Inc sub","altname":"ABCTools Inc sub","email":"jdoe@email.com","phone":"(512) 123-4567","subsidiary":"1"},"subcontact":{"datecreated":"12/10/2015 7:14 am","id":"5566142","entityid":"JohnDoe SubCustomer","firstname":"Johndoe","lastname":"Subcustomer","email":"jdoe@email.com","phone":"(512) 123-4567","company":null,"subsidiary":"1"}}
  </code></pre>
  
  <p>Below is how they should look when all merged together:</p>
  
  <pre><code>{
    "datecreated": "12\/10\/2015 9:18 am",
    "id": "5566447",
    "entityid": "652967804",
    "entitystatus": "13",
    "companyname": "ABCTools Inc parent",
    "altname": "ABCTools Inc parent",
    "email": "jdoe@email.com",
    "phone": "(512) 123-4567",
    "subsidiary": "1",
    "contact": {
      "datecreated": "12\/10\/2015 9:18 am",
      "id": "5566544",
      "entityid": "JohnDoe Customer",
      "firstname": "JohnDoe",
      "lastname": "Contact",
      "email": "jdoe@email.com",
      "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": "jdoe@email.com",
      "phone": "(512) 123-4567",
      "subsidiary": "1",
      "subcontact": {
        "datecreated": "12\/10\/2015 7:14 am",
        "id": "5566142",
        "entityid": "JohnDoe SubCustomer",
        "firstname": "Johndoe",
        "lastname": "Subcustomer",
        "email": "jdoe@email.com",
        "phone": "(512) 123-4567",
        "company": null,
        "subsidiary": "1"
      }
    }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>I've Googled around a lot, but and not finding anything specific to this issue...  Thanks for any insight into this.</p>