PowerShell 2.0 ConvertFrom-Json and ConvertTo-Json implementation exception "A circular reference was detected"
There are two problem in code:
- PowerShell enumerate collections, so instead of one array
ConvertFrom-Json20
return two dictionaries. It is not big deal by itself, but: return
statement in PowerShell v2 wrap returned objects byPSObject
. As result$o
contain array with twoPSObject
, 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.