title: Creating a nested JSON object dynamically?
body: <p>I am almost there with this but cannot seem to get this functionality going as planned.</p>
  
  <p>I have json 'jsonData' it contains formula of different terms</p>
  
  <pre><code>"jsonData":{
              "a" : "b + c",
              "b" : "d + e",
              "d" : "h + i",
              "c" : "f + g"
          }
  </code></pre>
  
  <p>What I am trying to do is to have a function pass one arguments 'mainItem'(ie. one of the key in 'jsonData' for example <code>a</code> in 'jsonData'). Within this function it will get the formula from the json data(for example a is the 'mainitem' and <code>b + c</code> is the formula) and check the dependency of child component of the formula i.e it will check whether <code>b</code> and <code>c</code> have any dependency down the line in json data. If it has any dependency it will be added as a child component to the parent for example if <code>b</code> have a formula in json data. <code>b</code> will be added as the 'mainitem' in the child component of the parent 'mainitem' <code>a</code>. At the end of the code, this is what I would like to get.</p>
  
  <pre><code>{
  mainitem : "a",
  formula : "b+c",
  childComponent: {
                   mainitem: "b",
                   formula : "d+e",
                   childcomponent: {
                                    mainitem: "d",
                                    formula : "h+i"
                                    }
                   },
                   {
                   mainitem: "c",
                   formula : "f+g"
                   },
  }
  </code></pre>
  
  <p>The issue is that I am able to create the parent object. But I have no idea how to create the child component for the parent and If the child component have sub child it will also embedded as the child of the child  component and so on. It is like a parent child hierarchy series</p>
  
  <pre><code>function getJson(mainItem) {
    var json = {};
    json['mainitem'] = mainItem;
    $.each(jsonData, function(key, value){
      if(mainitem == key){
        json['formula'] = value;
      }
    })
  }
  </code></pre>
  
  <p>Any insight into this would highly be appreciated. Thank you.</p>