title: Traversing JSON to buid a parent child output with Javascript
body: <p>I have to traverse the below input JSON to build a parent child relation as in the sample JSON.</p>
  
  <p>Logic, the element which has immediate_parent_id of -1 that becomes a root node. Than root node immediate_dependents are it's child. Now I have to pick up the each dependent of immediate_dependents and look for matching node_id and that particular elements immediate_dependents become its child. Like this I have to traverse till the nth Level of JSON. What's the optimal way of achieving this. I don't want to use jQuery. Looking out to be solved using javascript. </p>
  
  <p>Input JSON</p>
  
  <pre><code>[
     {
        "node_id":1,
        "token":"Google",
        "posTag":"NNP",
        "depLabel":"compound",
        "node_role":{
  
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":2,
           "name":"News"
        },
        "entity_tag":{
           "originalTag":"B-ORG",
           "prefix":"B",
           "position":"begin",
           "mainCategory":"ORG",
           "entityDescription":"Organization",
           "examples":"Government Agency, Broadcaster, Company, Educational Institution, Employers Organization, Non-Profit Organization, Samba School, Sports League, Sports Team, Website"
        },
        "siblings":[
  
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           2,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":2,
        "parentVerbID":3
     },
     {
        "node_id":2,
        "token":"News",
        "posTag":"NNP",
        "depLabel":"nsubj",
        "node_role":{
           "parent_verb_id":3,
           "grandparent_verb_id":3,
           "role":"topic"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"be.01",
        "parent_node":{
           "parent_node":3,
           "name":"is"
        },
        "entity_tag":{
           "originalTag":"L-ORG",
           "prefix":"L",
           "position":"last",
           "mainCategory":"ORG",
           "entityDescription":"Organization",
           "examples":"Government Agency, Broadcaster, Company, Educational Institution, Employers Organization, Non-Profit Organization, Samba School, Sports League, Sports Team, Website"
        },
        "siblings":[
           6,
           12
        ],
        "dependents":[
           {
              "dependent":1,
              "name":"Google",
              "depLabel":"compound"
           }
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           3
        ],
        "immediate_dependents":[
           {
              "dependent":1,
              "name":"Google",
              "depLabel":"compound"
           }
        ],
        "isRoot":false,
        "immediate_parent_id":3,
        "parentVerbID":3
     },
     {
        "node_id":3,
        "token":"is",
        "posTag":"VBZ",
        "depLabel":"root",
        "node_role":{
           "parent_verb_id":3
        },
        "verb_info":[
           {
              "is_verb":true,
              "nodeID":3,
              "negation":null,
              "lemma":"be",
              "id":"be-109-1-1",
              "pbID":"be.01",
              "meaningUsage":"copula",
              "verb_synonyms":[
                 "be",
                 "prove",
                 "appear",
                 "seem"
              ]
           }
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":-1,
           "name":null
        },
        "entity_tag":null,
        "siblings":[
  
        ],
        "dependents":[
           {
              "dependent":2,
              "name":"News",
              "depLabel":"nsubj"
           },
           {
              "dependent":1,
              "name":"Google",
              "depLabel":"compound"
           },
           {
              "dependent":6,
              "name":"aggregator",
              "depLabel":"attr"
           },
           {
              "dependent":4,
              "name":"a",
              "depLabel":"det"
           },
           {
              "dependent":5,
              "name":"news",
              "depLabel":"compound"
           },
           {
              "dependent":7,
              "name":"and",
              "depLabel":"cc"
           },
           {
              "dependent":8,
              "name":"app",
              "depLabel":"conj"
           },
           {
              "dependent":9,
              "name":"developed",
              "depLabel":"acl"
           },
           {
              "dependent":10,
              "name":"by",
              "depLabel":"agent"
           },
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           },
           {
              "dependent":12,
              "name":".",
              "depLabel":"punct"
           }
        ],
        "pobj_dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
  
        ],
        "immediate_dependents":[
           {
              "dependent":2,
              "name":"News",
              "depLabel":"nsubj"
           },
           {
              "dependent":6,
              "name":"aggregator",
              "depLabel":"attr"
           },
           {
              "dependent":12,
              "name":".",
              "depLabel":"punct"
           }
        ],
        "isRoot":true,
        "immediate_parent_id":-1,
        "parentVerbID":-1
     },
     {
        "node_id":4,
        "token":"a",
        "posTag":"DT",
        "depLabel":"det",
        "node_role":{
           "role":"comment"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":6,
           "name":"aggregator"
        },
        "entity_tag":null,
        "siblings":[
           5,
           7,
           8,
           9
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           6,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":6,
        "parentVerbID":3
     },
     {
        "node_id":5,
        "token":"news",
        "posTag":"NN",
        "depLabel":"compound",
        "node_role":{
           "role":"comment"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":6,
           "name":"aggregator"
        },
        "entity_tag":null,
        "siblings":[
           4,
           7,
           8,
           9
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           6,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":6,
        "parentVerbID":3
     },
     {
        "node_id":6,
        "token":"aggregator",
        "posTag":"NN",
        "depLabel":"attr",
        "node_role":{
           "parent_verb_id":3
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"be.01",
        "parent_node":{
           "parent_node":3,
           "name":"is"
        },
        "entity_tag":null,
        "siblings":[
           2,
           12
        ],
        "dependents":[
           {
              "dependent":4,
              "name":"a",
              "depLabel":"det"
           },
           {
              "dependent":5,
              "name":"news",
              "depLabel":"compound"
           },
           {
              "dependent":7,
              "name":"and",
              "depLabel":"cc"
           },
           {
              "dependent":8,
              "name":"app",
              "depLabel":"conj"
           },
           {
              "dependent":9,
              "name":"developed",
              "depLabel":"acl"
           },
           {
              "dependent":10,
              "name":"by",
              "depLabel":"agent"
           },
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "pobj_dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           3
        ],
        "immediate_dependents":[
           {
              "dependent":4,
              "name":"a",
              "depLabel":"det"
           },
           {
              "dependent":5,
              "name":"news",
              "depLabel":"compound"
           },
           {
              "dependent":7,
              "name":"and",
              "depLabel":"cc"
           },
           {
              "dependent":8,
              "name":"app",
              "depLabel":"conj"
           },
           {
              "dependent":9,
              "name":"developed",
              "depLabel":"acl"
           }
        ],
        "isRoot":false,
        "immediate_parent_id":3,
        "parentVerbID":3
     },
     {
        "node_id":7,
        "token":"and",
        "posTag":"CC",
        "depLabel":"cc",
        "node_role":{
           "role":"comment"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":6,
           "name":"aggregator"
        },
        "entity_tag":null,
        "siblings":[
           4,
           5,
           8,
           9
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           6,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":6,
        "parentVerbID":3
     },
     {
        "node_id":8,
        "token":"app",
        "posTag":"NN",
        "depLabel":"conj",
        "node_role":{
           "role":"comment"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":6,
           "name":"aggregator"
        },
        "entity_tag":null,
        "siblings":[
           4,
           5,
           7,
           9
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           6,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":6,
        "parentVerbID":3
     },
     {
        "node_id":9,
        "token":"developed",
        "posTag":"VBN",
        "depLabel":"acl",
        "node_role":{
           "parent_verb_id":9,
           "role":"comment"
        },
        "verb_info":[
           {
              "is_verb":true,
              "nodeID":9,
              "negation":null,
              "lemma":"develop",
              "id":"develop-26.1",
              "pbID":"develop.02",
              "meaningUsage":"create",
              "verb_synonyms":[
                 "sculpt",
                 "cut",
                 "remake",
                 "shape",
                 "make",
                 "carve",
                 "build"
              ]
           }
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":6,
           "name":"aggregator"
        },
        "entity_tag":null,
        "siblings":[
           4,
           5,
           7,
           8
        ],
        "dependents":[
           {
              "dependent":10,
              "name":"by",
              "depLabel":"agent"
           },
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "pobj_dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           6,
           3
        ],
        "immediate_dependents":[
           {
              "dependent":10,
              "name":"by",
              "depLabel":"agent"
           }
        ],
        "isRoot":false,
        "immediate_parent_id":6,
        "parentVerbID":-1
     },
     {
        "node_id":10,
        "token":"by",
        "posTag":"IN",
        "depLabel":"agent",
        "node_role":{
           "parent_verb_id":9,
           "grandparent_verb_id":9,
           "role":"creator"
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"develop.02",
        "parent_node":{
           "parent_node":9,
           "name":"developed"
        },
        "entity_tag":null,
        "siblings":[
  
        ],
        "dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "pobj_dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           9,
           6,
           3
        ],
        "immediate_dependents":[
           {
              "dependent":11,
              "name":"Google",
              "depLabel":"pobj"
           }
        ],
        "isRoot":false,
        "immediate_parent_id":9,
        "parentVerbID":9
     },
     {
        "node_id":11,
        "token":"Google",
        "posTag":"NNP",
        "depLabel":"pobj",
        "node_role":{
  
        },
        "verb_info":[
  
        ],
        "action_verb_roleset":"",
        "parent_node":{
           "parent_node":10,
           "name":"by"
        },
        "entity_tag":null,
        "siblings":[
  
        ],
        "dependents":[
  
        ],
        "pobj_dependents":[
  
        ],
        "dobj_dependents":[
  
        ],
        "nobj_dependents":[
  
        ],
        "parents":[
           10,
           9,
           6,
           3
        ],
        "immediate_dependents":[
  
        ],
        "isRoot":false,
        "immediate_parent_id":10,
        "parentVerbID":9
     }
  ]
  </code></pre>
  
  <p>Sample JSON Output(to render the d3 tree)</p>
  
  <pre><code>   {  
     "name":"is",
     "children":[  
        {  
           "name":"news",
           "children":[  
              {  
                 "name":"Google",
                 "children":[  
  
                 ]
              },
              {  
                 "name":"aggregator",
                 "children":[  
                    {  
                       "name":"a",
                       "children":[  
  
                       ]
                    },
                    {  
                       "name":"news",
                       "children":[  
  
                       ]
                    },
                    {  
                       "name":"app",
                       "children":[  
  
                       ]
                    },
                    {  
                       "name":"developed",
                       "children":[  
                          {  
                             "name":"by"
                          }
                       ]
                    }
                 ]
              },
              {  
                 "name":".",
                 "children":[  
  
                 ]
              }
           ]
        }
     ]
  }
  </code></pre>