0

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.

5
  • You need to put $ in front of false and also use -eq for comparisons like so: If(JSON.response -eq $false){ Commented Dec 12, 2016 at 16:30
  • I gave it a shot, it's still continuing to my elseif condition, even though response is returning with nothing besides {}. Commented Dec 12, 2016 at 16:45
  • 1
    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. Commented Dec 12, 2016 at 16:47
  • Pardon my ignorance, but why are you saying json.response instead of $json.response ? Isn't json a variable? Commented Dec 12, 2016 at 16:49
  • Hey Jeff, I thought that too and it works with a handful of other apis. But, in this case response_code only returns information on if the response is error free. Although I could be mistaken. Also Walter it was a syntax error on my part. I'll fix it once I'm back at my desk. Thanks guys. Commented Dec 12, 2016 at 16:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sounds like he will need to test both values response and resonse_code using -and. Might be useful to show what the if statement looks like since there is a couple of errors here to address.
@Matt: Good point, but the OP's code isn't really detailed enough to show context for using $json.response. The best I can do at the moment is to recast what he's got there; I'd simply write it if ($json.response_code = 1) { # Do the code } and omit the test where he does nothing entirely.
@JeffZeitlin Hello, Jeff I appreciated the response. I do see your point and this would work with other RESTful apis. However, in this case response_code merely means http status 200. Which also means that even if there is no data on the item I am querying for, it will still return as "0".
1

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'}

Comments

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.