PowerShell does not support JSON Data Types

by May 12, 2014

By default, any object created from JSON uses String as a data type:

$json = @"
{
    "Name": "Weltner",
    "ID" : "123"
 }
"@

$info = ConvertFrom-Json -InputObject $json
$info.Name
$info.ID

JSON does support data types, though, although JSON data types are not identical to .NET data types. Note in the following code how "ID" is declared as "number", and how a number is assigned that no longer needs quoting.

$json = @"
{
    "Name": "Weltner",
    number: "ID" : 123
 }
"@

$info = ConvertFrom-Json -InputObject $json
$info.Name
$info.ID

However, when using ConvertFrom-Json, it turns out that PowerShell is not honoring the data type declaration. It always turns values into String data.

Twitter This Tip! ReTweet this Tip!