Skip to content

Powershell Core deserializes numbers in JSON as Int64 vs Windows Powershell which does it as Int32 #14264

@iRon7

Description

@iRon7

This is actually a copy of the StackOverflow issue with the same name from @Mark:
Powershell Core deserializes numbers in JSON as Int64 vs Windows Powershell which does it as Int32

Steps to reproduce

$a = "1" | ConvertFrom-Json
(@{ $a = 2 }).1

Expected behavior

Return 2 (just like Windows PowerShell 5)

Actual behavior

Returns nothing (because the actual key is of type [Int64] and the key ".1" of type [Int32])

I am not sure whether this can be called a bug or is "by design" but my expectation (from a dynamically typed language as PowerShell Core) is that a Json number (less then [int]::MaxValue) should default to an [Int32] type, just like:

$a = 1
$a.GetType().Name
Int32

Workarround

Recast the number:

$a = "1" | ConvertFrom-Json
$a = 0 + "$a"
(@{ $a = 2 }).1
2

related:

Environment data

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.19042
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

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-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