1
$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?

3
  • 1
    JSON is a string representation. You'd need to convert it to a collection of objects to check properly, using ConvertFrom-Json Commented Jul 31, 2021 at 23:39
  • 1
    From that JSON the condition would be if($json.result.wallets.name -contains 'W1'){...} Commented Jul 31, 2021 at 23:42
  • @SantiagoSquarzon It doesn't work Commented Jul 31, 2021 at 23:44

2 Answers 2

3

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"
}
Sign up to request clarification or add additional context in comments.

3 Comments

($o.wallets.name -contains "W1") in if condition works from shell when $o is pastebin.com/raw/nibSGKpM But it doesn't work when I send a POST request to API with ConvertFrom-Json it returns error: ConvertFrom-Json : Invalid JSON primitive: and without it $o =$w is pastebin.com/raw/hMqNVXtv. Something wrong with API or I need to use something else when using POST request? Tried lot of things but nothing works
I have shared the script that worked for me in the answer
That looks like a completely different issue. At a glance, it looks like $w is your wallets array already, so you would just do $w.name -contains "W1", but you'd have to post a full question with how you are setting $w
0

Not sure how did it get resolved but below script works:

if ($w.Contains("W1")) {
    Write-Host "Wallet 'W1' exists"
}

2 Comments

You changed from using $w.wallets.name -contains "W1" to $w.Contains("W1"). -contains and .Contains() work differently.
This is not good... using the string method .Contains() on the whole JSON string may return $true also when some other element or even element Name happens to contain the (sub)string "W1". You really need to use the -contains or -in operator to check for that value in an array. As @GarrGodfrey commented, do $wallets.result.wallets.name -contains 'W1' or alternatively 'W1' in $wallets.result.wallets.name. (if you need case sensitive matching, use -ccontains or -cin)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.