-
Notifications
You must be signed in to change notification settings - Fork 8.2k
ConvertTo-Json doesn't serialize $null #10942
Copy link
Copy link
Closed
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module
Description
Conceptually related: #9231
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]'Environment data
PowerShell Core 7.0.0-preview.5
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module