$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json[$key]
I'd like that to print true but it doesn't. I know $json.key would work. Can this be done?
If you know that $json.key would work, then why you're switching from dot to square brackets?
All these will work:
$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json.$key
Write-Host $json.$($key)
Write-Host $json."$key"
key is read from somewhere else. $json.$key works nicely... I had no idea PS could understand that. Thanks!You could be referencing it using dot notation with your variables.
$json.$key
So in your Write-Host you would need a subexpression if you used quotes in your write-host
Write-Host "Key is: $($json.$key)"
You were trying to use array notation and returning null.