title: Creating a tree using json from a list/table
body: <p>Let’s say I have table/list like this n=3 in this case, but n can be as unlimited. </p>
  
  <pre><code>groupid       answerid1     answerid2     answerid(n)
  1              3            6             8 
  1              3            6             9 
  1              4            7               
  2              5                            
  </code></pre>
  
  <p>and i want to create a parent/child tree json  output like this using java.(I have been using GSON)</p>
  
  <pre><code>    {
          data: [
              {
                  groupid: 1,
                  children: [
                      {
                          answerid1: 1,
                          children: [
                              {
                                  answerid2:3,
                                  children: [
                                             {
                                      answerid3:6,
                                                    children: [
                                                                {answerid4: 8},
                                    {answerid4: 9} 
                                                               ]
                                                } 
  
  
                              }, {
                                   answerid2: 4,
                                   children: [
                                            {answerid3:7} 
                                     ]
                                   }
                                  ]         
                      }, 
  
                 {
                   groupid1: 2,
                   children: [
                         { answerid2: 5}
                          ]
                  }
  
                 ]      
          }
  </code></pre>
  
  <p>what would be the code/steps to do so.  i have looked through lots of tags but mostly people are printing the output and not recursively build a hashmap/ArrayList for GSON to parse adn write to API.  one other point each id has other data associated with it that will have to be included in the json output.   for instance instead of {groupid:1} would need to this {groupid:1, text=toyota}.</p>
  
  <p>any help is greatly appreciated as i am fairly new to java as i come from SAS background. </p>
  
  <p>I get data like this (just a matrix of list) 
  Toyota, Gas, Compact, Corolla <li>
  Toyota, Gas, Compact, Camry  <li>
  Toyota, Hybrid, Compact, Prius<li>
  Honda, Gas, Compact, Civic <li></p>
  
  <p>If needed I can REFORMAT THE DATA into two tables</p>
  
  <p>parentId    parText     answerId<li>
  1       Toyota      1<li>
  1       Toyota      2<li>
  1       Toyota      3<li>
  2       Honda       4<li></p>
  
  <p>answerId    level   answerTextid    answerText<li>
  1       1   1       Gas<li>
  1       2   2       Compact<li>
  1       3   3       Corolla<li>
  2       1   1       Gas<li>
  2       2   2       Compact<li>
  2       3   4       Camry<li>
  …</p>
  
  <p>Then I need to make it a tree(nested output like the JSON shows with parent/children - just like if you were creatign a file system directory)</p>
  
  <p>one other thign i would like to do is for each car have mileage as a varialbe  ({answerid3:4, text=Corolla, mileage=38}.  but also if i traverse up the tree give an average mile for the branch.  Like say at branch Toyota, Gas, Compact the mileage would be avg(Camry, Corolla)</p>
  
  <p>the output is a little off, i am looking for something like this. if no children then no children arraylist, and attrbutes are part of one object (hashmap)</p>
  
  <pre><code>{"data":[{"id":1,"children":
      [{"id": 2,"children":
          [{"id": 3 ,"children":
              [{"id": 4,"name":"Prius"}],"name":"Compact"}],"name":"Hybrid"},
      {"id":5,"children":
          [{"id":3,"children":
              [{"id":7,"MPG":38, "name":"Corolla"},
               {"id":8,"MPG":28,"name":"Camry"}],"name":"Compact"}],"name":"Gas"}],"name":"Toyota"},
  {"id":9, "children":
      [{"id":10,"children":
          [{"id":3 ,"children":
              [{"id":11 ,"name":"Civic"}],"name":"Compact"}],"name":"Gas"}],"name":"Honda"}]}
  </code></pre>