PowerShell: Retrieve the JSON object by variable value

$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json[$key]

I want it to be printed, but it is not the case. I know that $json.key will work. Can it be done?

If you know that $json.key works, then why switch from dot to square brackets?
All of these are valid:

$json = ConvertFrom-Json "{key:true}"
$key = "key"

Write-Host $json.$key
Write-Host $json.$($key)
Write-Host $json."$key"

$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json[$key]

I want it to be printed, but it is not the case. I know that $json.key will work. Can it be done?

If you know that $json.key works, then why switch from dot to square brackets?
All of these are valid:

$json = ConvertFrom-Json "{key:true}"
$key = "key"

Write-Host $json.$key
Write-Host $json.$($key)
Write-Host $json."$key"

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2706 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.