title: umbraco content tree nodes to json format
body: <p>I have an umbraco website with the following content structure:
  (the text in brackets is the nodeTypeAlias)</p>
  
  <pre><code>[root]
   - [child]
   |  - [module]
   |  |  - [submodule]
   |  |  - [submodule]
   - [child]
   |  - [module]
   |  - [module]
   |  |  - [submodule]
  </code></pre>
  
  <p>I am trying to export the above structure (together with the node's properties) into the following json file:</p>
  
  <pre><code>{
      "root": {
          "child": [
              {
                  "id": "1",
                  "name": "Child 1",
                  "module": [
                      {
                          "id": "2",
                          "name": "Module 1",
                          "submodule": [
                              {
                                  "id": "3",
                                  "name": "Sub module 1"
                              },
                              {
                                  "id": "4",
                                  "name": "Sub module 3"
                              }
                          ]
                      }
                  ]
              },
              {
                  "id": "5",
                  "name": "Child 5",
                  "module": [
                      {
                          "id": "8",
                          "name": "Module 8"
                      },
                      {
                          "id": "6",
                          "name": "Module 6",
                          "submodule": [
                              {
                                  "id": "7",
                                  "name": "Sub module 7"
                              },
                              {
                                  "id": "9",
                                  "name": "Sub module 9"
                              }
                          ]
                      }
                  ]
              }
          ]
      }
  }
  </code></pre>
  
  <p>So far I've wrote down the following code in Linqpad but the result is not the one that I was looking for.</p>
  
  <pre><code>List&lt;Node&gt; brands = new List&lt;Node&gt;()
  {               
      new Node
      {
           id = 1,
          name = "Brand 1",
          type = "brand",
          children = new List&lt;Node&gt;
          {        
              new Node
              {
                  id = 2,
                  name = "Shelf 1",
                  type = "shelf",
                  children = new List&lt;Node&gt;
                  {
                      new Node
                      {
                          id = 1,
                          name = "Bookcase 1",
                          type = "bookcase"
                      },
                      new Node
                      {
                          id = 2,
                          name = "Bookcase 2",
                          type = "bookcase"
                      },
                      new Node
                      {
                          id = 3,
                          name = "Bookcase 3",
                          type = "bookcase"
                      }
                  }
              },
              new Node
              {
                  id = 3,
                  name = "Shelf 2",
                  type = "shelf",
                  children = new List&lt;Node&gt;
                  {
                      new Node
                      {
                          id=1,
                          type= "module",
                          name = "Module 1"
                      },
                      new Node
                      {
                          id=2,
                          type= "pdf",
                          name = "PDF 1"
                      },
                      new Node
                      {
                          id=3,
                          type= "link",
                          name = "Link 1"
                      },
                      new Node
                      {
                          id=4,
                          type= "link",
                          name = "Link 2"
                      },
                  }
              }                   
          }
      },
  
      new Node
      {
          id = 2,
          name = "Brand 2",
          type = "brand",
          children = new List&lt;Node&gt;
          {
              new Node
              {
                  id = 2,
                  name = "Shelf 1",
                  type = "shelf",
              },
              new Node
              {
                  id = 3,
                  name = "Shelf 2",
                  type = "shelf",
              }
          }
      }
  };
  
  
  Result container = new Result();
  
  Action&lt;List&lt;Node&gt;, Result&gt; add = null;
  add = (nodes, coll) =&gt;
  {
      List&lt;Result&gt; list = null;
      if(nodes != null &amp;&amp; nodes.Count &gt; 0)
      {
          nodes.Dump("nodes");
          foreach(var node in nodes)
          {
              string key = node.type;
              list = null;
              if(coll.Children.ContainsKey(key))
              {
                  list = coll.Children[key];
              }
              else
              {
                  list = new List&lt;Result&gt;();
              }
              Result r = new Result();
              r.Name = node.name;
              add(node.children, r);
              list.Add(r);
              coll.Children[key] = list;
              coll.Dump("coll");
          }
      }
  };
  
  add(brands, container);
  container.Dump();
  var serializer = new JavaScriptSerializer();
  serializer.Serialize(container).Dump();
  }
  
  public class Result
  {
      public Result()
      {
          this.Children = new Dictionary&lt;string, List&lt;Result&gt;&gt;();
          this.Properties = new Dictionary&lt;string, string&gt;();
      }
      public string Name {get;set;}
      public Dictionary&lt;string, string&gt; Properties {get;set;}
      public Dictionary&lt;string, List&lt;Result&gt;&gt; Children {get;set;}
  }
  
  public class Node
  {
      public string name{get;set;}
      public string type {get;set;}
      public int id {get;set;}
      public List&lt;Node&gt; children{get;set;}
  </code></pre>
  
  <p>result:</p>
  
  <pre><code>{
      "Name": null,
      "Properties": {},
      "Children": {
          "brand": [
              {
                  "Name": "Brand 1",
                  "Properties": {},
                  "Children": {
                      "shelf": [
                          {
                              "Name": "Shelf 1",
                              "Properties": {},
                              "Children": {
                                  "bookcase": [
                                      {
                                          "Name": "Bookcase 1",
                                          "Properties": {},
                                          "Children": {}
                                      },
                                      {
                                          "Name": "Bookcase 2",
                                          "Properties": {},
                                          "Children": {}
                                      },
                                      {
                                          "Name": "Bookcase 3",
                                          "Properties": {},
                                          "Children": {}
                                      }
                                  ]
                              }
                          },
                          {
                              "Name": "Shelf 2",
                              "Properties": {},
                              "Children": {
                                  "module": [
                                      {
                                          "Name": "Module 1",
                                          "Properties": {},
                                          "Children": {}
                                      }
                                  ],
                                  "pdf": [
                                      {
                                          "Name": "PDF 1",
                                          "Properties": {},
                                          "Children": {}
                                      }
                                  ],
                                  "link": [
                                      {
                                          "Name": "Link 1",
                                          "Properties": {},
                                          "Children": {}
                                      },
                                      {
                                          "Name": "Link 2",
                                          "Properties": {},
                                          "Children": {}
                                      }
                                  ]
                              }
                          }
                      ]
                  }
              },
              {
                  "Name": "Brand 2",
                  "Properties": {},
                  "Children": {
                      "shelf": [
                          {
                              "Name": "Shelf 1",
                              "Properties": {},
                              "Children": {}
                          },
                          {
                              "Name": "Shelf 2",
                              "Properties": {},
                              "Children": {}
                          }
                      ]
                  }
              }
          ]
      }
  }
  </code></pre>
  
  <p>Any idea?</p>
  
  <p>Thanks.</p>