Reputation: 86967
I'm trying to use PowerShell to determine if a single property in a JSON array are all the same.
This is my JSON:
{
"project": 1,
"info": {
"things": [
{ "thingId": 1, "status": "success" },
{ "thingId": 2, "status": "success" },
{ "thingId": 3, "status": "failure" }
]
}
}
Given that JSON payload, I'm trying to return TRUE
or FALSE
if:
things
is null/empty - TRUE
(nothing to check, so we're OK).things
exists AND has some data ... and all the status
properties equal "success"
- TRUE
.FALSE
For this example, lets use this syntax:
(Invoke-RestMethod -Uri 'https://my.api.com/something/here' -Method Get).info.<not sure of the rest>
Upvotes: 0
Views: 389
Reputation: 200323
Seems pretty straightforward to me (as long as by "null/empty" you mean an empty array or string, not an empty object).
$json = Invoke-RestMethod -Uri 'https://my.api.com/something/here' -Method Get
-not $json.info.things -or $json.info.things.status -notcontains 'failure'
If you're stuck with PowerShell v2 replace the second clause with
@($json.info.things | Select-Object -Expand status) -notcontains 'failure'
If there can be states other than success
and failure
replace it with something like this:
@($json.info.things | ? {$_.status -eq 'success'}).Count -eq $json.info.things.Count
Upvotes: 1