You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Null values are a legitimate part of JSON, so ConvertTo-Json should be able to serialize them.
Currently, ConvertTo-Json quietly ignores $nulls as a distinct input objects (but does serialize them correctly as a property values).
Steps to reproduce
# OK
[pscustomobject] @{ prop=$null } |ConvertTo-Json-Compress | Should -Be '{"prop":null}'# FAIL$null|ConvertTo-Json-Compress | Should -Be 'null'ConvertTo-Json-Compress $null| Should -Be 'null'
Expected behavior
All tests should pass.
Actual behavior
The $null tests fail:
^ Expected 'null', but got $null
That is, instead of returning string 'null', there was no output.
Update: To clarify: $null is generally ignored, so the following test fails too, because it returns '[1,2]', not '[1,null,2]':
1,$null,2|ConvertTo-Json-Compress | Should -Be '[1,null,2]'
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues
Activity
SteveL-MSFT commentedon Oct 30, 2019
So you expect: '{"":null}' to come out?
mklement0 commentedon Oct 30, 2019
Steve Lee (@SteveL-MSFT): No, it should be
'null', as used in the tests.SteveL-MSFT commentedon Oct 30, 2019
This should be a fairly simple change, but it also seems like an extreme edge case.
mklement0 commentedon Oct 30, 2019
Perhaps a single
$nullis rare, but what about something like1, $null, 2 | ConvertTo-Json, which quietly discards the$null?Consider what JavaScript does:
JSON.stringify(null)->'null'JSON.stringify([1, null, 2])->'[1, null, 2]'