1

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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
  • 1
    It seems to be an issue with `-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 Jul 11 '19 at 18:49
  • 1
    ConvertTo-Json's default depth of 2 with no warning has been talked about often. – js2010 Jul 11 '19 at 18:54
  • @js2010 Vote to close as duplicate please. I wish I had found such "often talked about" discussion *much* sooner in my day.. – user2864740 Jul 11 '19 at 18:55
  • https://stackoverflow.com/a/22260605/2864740 , https://stackoverflow.com/a/32775099/2864740 – user2864740 Jul 11 '19 at 18:55
  • 1
    Possible duplicate of [Unexpected ConvertTo-Json results? Answer: it has a default -Depth of 2](https://stackoverflow.com/questions/53583677/unexpected-convertto-json-results-answer-it-has-a-default-depth-of-2) – iRon Jul 11 '19 at 19:22

1 Answers1

1

A little bit of PEBKAC, a little bit of Why Is That The Behavior?!

It seems to be an issue with -Depth and the pruning logic. Setting a "higher depth" results in round-trip behavior working as expected. Having the truncation end as a string, as opposed to say null, seems unfortunate - although possibly consistent if one finds that "To String" is the correct termination.

Change to "" (unexpected):

PS> '{"a":[{"b":{}}]}' | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 2
'{"a":[{"b":""}]}'

Round-trip (expected):

PS> '{"a":[{"b":{}}]}' | ConvertFrom-Json | ConvertTo-Json -Compress -Depth 3
'{"a":[{"b":{}}]}'
user2864740
  • 60,010
  • 15
  • 145
  • 220