title: Javascript: How to change JSON structure (Traverse all the Nodes of a JSON Object)
body: <p>I'd like to restructure some JSON data into a different format, for convenience. The format in which I get it from the server is inconvenient for me; all of the items are in one node, but some of them have <code>parent: id</code> which tells us the parent for that item.</p>
  
  <p>I'd like to take the JSON structure from <code>JsonFromSrver</code> and restructure to look like <code>resultJson</code>. I also need to be able to send the data back to the server in the original format after making modifications.</p>
  
  <pre><code>var JsonFromSrver = [
          {
              "id": 1,
              "type": "folder",
              "title": "HeadItem1",
          },
          {
              "id": 2,
              "type": "folder",
              "title": "HeadItem2",
          },
          {
              "id": 33,
              "parent": 1,
              "type": "file",
              "title": "ChildItem",
  
          },
          {
              "id": 103,
              "parent": 2,
              "type": "file",
              "title": "ChildItem"
          }
  ]
  
  var resultJson = [
          {
              "id": 1,
              "type": "folder",
              "title": "HeadItem1",
              "children": 
                 [{
                    "id": 33,
                    "parent": 1,
                    "type": "file",
                    "title": "ChildItem"
                }]
          },
          {
              "id": 2,
              "type": "folder",
              "title": "HeadItem2",
              "children": 
                 [{
                    "id": 103,
                    "parent": 2,
                    "type": "file",
                    "title": "ChildItem"
                }]
          }
  ]
  </code></pre>