Skip to content

ConvertTo-Json outputs IPAddress as object instead of string, with inconsistent properties #26636

@yotsuda

Description

@yotsuda

Prerequisites

Summary

ConvertTo-Json produces different JSON output for the same IPAddress value depending on whether it is passed via pipeline or via -InputObject parameter. The pipeline output includes an IPAddressToString property, while the -InputObject output does not.

Steps to reproduce

$ip = [System.Net.IPAddress]::Parse("192.168.1.1")

# Via pipeline
$ip | ConvertTo-Json -Compress

# Via -InputObject parameter
ConvertTo-Json -InputObject $ip -Compress

Expected behavior

# Both methods should produce the same output
"192.168.1.1"

Note: Serializing as a string would require a custom JsonConverter. Similar types (e.g., IPEndPoint, PhysicalAddress) would also need the same treatment, so the scope of this change may be larger than it appears.

Actual behavior

# Via pipeline (includes IPAddressToString)
{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"IPAddressToString":"192.168.1.1"}

# Via -InputObject (no IPAddressToString)
{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952}

Environment data

Name                           Value
----                           -----
PSVersion                      7.5.1
PSEdition                      Core
GitCommitId                    7.5.1
OS                             Microsoft Windows 10.0.26100
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    In-Progress-Issues

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions