Skip to content

ConvertTo-Json doesn't serialize $null #10942

@mklement0

Description

@mklement0

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

Metadata

Metadata

Assignees

Labels

Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions