$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"
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With