PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation exception "A circular reference was detected" PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation exception "A circular reference was detected" json json

PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation exception "A circular reference was detected"


There are two problem in code:

  1. PowerShell enumerate collections, so instead of one array ConvertFrom-Json20 return two dictionaries. It is not big deal by itself, but:
  2. return statement in PowerShell v2 wrap returned objects by PSObject. As result $o contain array with two PSObject, and JSON serializer can not work with them properly.

To prevent collection enumeration you can use unary comma operator. This operator create array with single element, and array will be enumerated instead of collection. And since PowerShell return output even without return statement, you can just remove it.