I have a JSON (array of objects) obtained via Invoke-RestMethod and I try to iterate trough them using foreach.
The property I’m interested to use as a filter is named "$value" and I cannot change this, being a COTS application.
foreach($item in $Result.value) {
if ($item.properties.threadType) {
Write-Host $item.properties.threadType
if ($item.properties.threadType.$value -eq "1234567") {
Write-Host $item.id
}
}
}
Output of Write-Host $item.properties.threadType, if statements evaluates as false.
@{$type=System.String; $value=18792098}
@{$type=System.String; $value=N/A}
@{$type=System.String; $value=1234567}
Snippet of that JSON:
...
"properties": {
"threadType": {
"$type": "System.String",
"$value": "1234567"
}
},
...
How can I access $value property?
I’ve tried using
$item.properties.threadType.$value$item.properties.threadType.value- even
$item.properties.threadType['$value']
>Solution :
You can use quotation marks to qualify a member name – in this case you’ll want to use single-quotes to avoid expansion of $value as a variable expression:
if ($item.properties.threadType.'$value' -eq "1234567") { <# ... #> }