Using Windows PowerShell terminal for running a PS script.
I need to do (among the rest) some json file parsing. I ended up in a strange situation, here below posting a minimal example:
Json file:
{
"mykey": "myvalue"
}
PS script:
$jsonContent = Get-Content "myfile.json" -Raw | ConvertFrom-Json
$jsonContent.GetType().FullName
$key = "mykey"
Write-Host "Using bracket notation: $($jsonContent[$key])"
Write-Host "Using dot notation: $($jsonContent.$key)"
Output:
System.Management.Automation.PSCustomObject
Using bracket notation:
Using dot notation: myvalue
I expected the exact opposite, bracket notation to work and dot notation to not work: what's happening here?
As an additional detail, $PSVersionTable.PSVersion returns 5.1.22621.4391.
IDictionaryinterface (e.g.,([pscustomobject] @{ Foo = 42 }).Foo). For instances of types that do implement theIDictionaryinterface, such as[hashtable], PowerShell suports index notation and dot notation interchangeably (e.g.,@{ Foo = 42 }['Foo']or@{ Foo = 42 }.Foo).