Skip to content

ConvertTo-Json unexpectedly serializes AutomationValue.Null as an empty object, not null #9231

@mklement0

Description

@mklement0

PowerShell commands that output nothing output the special [System.Management.Automation.Internal.AutomationNull]::Value singleton, which typically behaves like $null.

Unexpectedly, ConvertTo-Json doesn't treat it as $null, but treats it as an empty object, which is undesired.

Steps to reproduce

# Note: `. {}` yields the [System.Management.Automation.Internal.AutomationNull]::Value
# singleton, not $null.
[pscustomobject] @{ foo = . {}; bar = $null } | ConvertTo-Json

Expected behavior

{
  "foo": null,
  "bar": null
}

Actual behavior

{
  "foo": {},
  "bar": null
}

Environment data

PowerShell Core 6.2.0-rc.1
Windows PowerShell v5.1 

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-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