PowerShell is unable to reliably round-trip JSON by default. How can I ensure that such JSON is correctly round-tripped?
Here is a minimal example of the broken round-trip serialization:
PS> '{"a":[{"b":{}}]}' | ConvertFrom-Json | ConvertTo-Json -Compress
{"a":[{"b":""}]}
The unexpected change from {}
to ""
results in JSON which is no longer valid.
This is under version 5.1:
PS> $PSVersionTable.PSVersion.ToString()
5.1.15063.674
Similarly, '[{"b":{}]' | ConvertFrom-Json | ConvertTo-Json
is also questionable, as discussed in https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/15123162-convertto-json-doesn-t-serialize-simple-objects-pr. However, consider that questionable nature not covered in this question.
-Depth
. Setting a "higher depth" results in round-trip behavior working as expected. Having the depth end as 'a string', as opposed to say,null
, is very unfortunate. – user2864740