title: Json circular reference in powershell 2.0 with javascriptSerializer
body: <p>I´m writing a script in powershell 2.0 and an upgrade to 3.0 or higher is not possible right now. In this script I try to serialize some data to JSON with the code from this link (<a href="https://stackoverflow.com/questions/28077854/powershell-2-0-convertfrom-json-and-convertto-json-implementation">PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation</a>):</p>
  
  <pre><code>function ConvertTo-Json20([object] $item){
      add-type -assembly system.web.extensions
      $ps_js=new-object system.web.script.serialization.javascriptSerializer
      return $ps_js.Serialize($item)
  }
  </code></pre>
  
  <p>My problem is that I somehow get a circular reference and I really don´t know why. I set up a litte piece of test data and the structure looks in powershell like this:</p>
  
  <pre><code>$testRoot = @{
      "id" = "1"
      "children" = @( 
          @{
              "id" = "2"
              "children" = @( 
                  @{
                      "id" = "2";
                  };
                  @{
                      "id" = "3";
                  }
              );
          };
          @{
              "id" = "4"
              "children" = @( 
                  @{
                      "id" = "5";
                  }
              );
          }
      )
  }
  </code></pre>
  
  <p>I know it looks junky, but I just need it in this format.</p>
  
  <p>The structures I need to serialize have a few more layers, so even more "children" and there is the point where it gets strange.</p>
  
  <p>When I try this:</p>
  
  <pre><code>ConvertTo-Json20 $testRoot
  </code></pre>
  
  <p>everything works fine. The structure gets parsed like this:</p>
  
  <pre><code>{
     "id":"1",
     "children":[
          {
              "id":"2",
              "children":[
                 {
                     "id":"2"
                 },
                 {
                     "id":"3"
                 }
              ]
          },
          {
              "id":"4",
              "children":[
                 {
                     "id":"5"
                 }
              ]
          }
     ]
  }
  </code></pre>
  
  <p>But now comes the problem. As mentioned the structure has more layers, so I try this which just sets the data in an array.</p>
  
  <pre><code>ConvertTo-Json20 @($testRoot)
  </code></pre>
  
  <p>But it does not work I just get an error message saying:</p>
  
  <pre><code>Exception in method "Serialize" with 1 argument(s):  
  "While serializing an object of type "System.Management.Automation.PSParameterizedProperty" a circular reference was discovered."
  At C:\Users\a38732\Desktop\Temp.ps1:34 symbol:28
  +     return $ps_js.Serialize &lt;&lt;&lt;&lt; ($item)
  + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : DotNetMethodException
  </code></pre>
  
  <p>(I translated the error message from german, so there might be some words different in the english version...)</p>