Skip to content

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

Closed

Description

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 

Activity

  1. added
    Issue-Question
    on Mar 26, 2019
  2. vexx32 commented on Mar 26, 2019

    @vexx32
    Collaborator

    cc Mark Kraus (@markekraus)

    This is... weird lol.

  3. cawoodm commented on Mar 26, 2019

    @cawoodm

    Also . {} is equal (-eq) to $null but doesn't serialize to null.

  4. markekraus commented on Mar 27, 2019

    @markekraus
    Contributor

    heh.. that's a fun one... we may need to create a custom JSON handler for AutomationNull or something.

  5. added
    Issue-Bug
    and removed
    Issue-Question
    on Mar 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions