Reputation: 123
$w = $wallets | ConvertTo-Json -Depth 3
{
"result": {
"wallets": [
{
"name": "W1"
},
{
"name": "W2"
},
{
"name": "W3"
},
{
"name": "W4"
}
]
},
"error": null,
"id": "test"
}
I want to check if the JSON response above contains wallet name "W1". So I wrote this in PowerShell which doesn't work:
if ($w.wallets.name -contains "W1") {
Write-Host "Wallet 'W1' exists"
}
How do I check something in above JSON using PowerShell?
Upvotes: 1
Views: 4057
Reputation: 123
Not sure how did it get resolved but below script works:
if ($w.Contains("W1")) {
Write-Host "Wallet 'W1' exists"
}
Upvotes: 0
Reputation: 9001
JSON is a string. -contains won't work for it. You need to convert your JSON to an object.
In your example, you would just operate on $wallets
and not even convert it to JSON, but let's assume your JSON actually comes from somewhere else.
$o = $w | ConvertFrom-Json
Now, you can operate on it like an object:
if ($o.result.wallets.name -contains "W1") {
Write-Host "Wallet 'W1' exists"
}
Upvotes: 3