Alexander Sinno
Alexander Sinno

Reputation: 564

Powershell json RESTful api - Conditional Statement

Currently, I am attempting to write a conditional statement that states the following

If($JSON.response = false){
'Dont do anything and continue to the next block of code.'
}

Right now the JSON returns like this when there is no data:

 response_code response
 ------------- --------
             0 {} 

This is what I have so far:

If($json.response = 0){
'Dont do anything here'
}elseif($json.response = 1){
'Do the code'
}

I'd like to add that response_code is always equal to 0 when the response is error free as in status code 200. However, when there are no fields returned response is just an empty hashtable.

Upvotes: 0

Views: 307

Answers (2)

Alexander Sinno
Alexander Sinno

Reputation: 564

I found the answer since the object is an array it was best to roll out like this.

 If($json.object.Count -eq 0){
 'dont do the code'
 }Else{
 'Do the code'}

Upvotes: 1

Jeff Zeitlin
Jeff Zeitlin

Reputation: 10799

Do you really want to be testing json.response rather than json.response_code? The numeric comparison suggests that you should be testing json.response_code.

Also, if json is a structure that is stored in a variable, you should be testing it as $json.response_code - note the $ - and using the -eq comparison operator as @JamesC. noted.

Finally, unless you're likely in the future to change what you do in the event that $json.response_code is zero, just drop that test entirely, and only test for $json.response_code values where you actually do something.

Upvotes: 2

Related Questions