Reputation: 115
I'm trying to use regular expressions on PowerShell, but I'm with difficult to solve this question.
I have the variable
$appstatus = az webapp list --query "[].{state: state}"
$result = $appstatus -Match "Running"
The result of $appstatus is in JSON format
[
{
"state": "Running"
}
]
How can store the result to be "True" or "False"
Upvotes: 0
Views: 91
Reputation: 115
To solve my question, I use the commands below.
$azwebapp = az webapp list --query "[].{state: state}"
$appstatus = $azwebapp | ConvertFrom-Json
$result = $appstatus.state
Upvotes: 0
Reputation: 6312
You should convert it to json instead of using regular expressions. Never implement you own json parsing. Here is a way to do it:
$input = @'
[
{
"state" : "Running"
}
]
'@
# Convert it to Json
$json = ConvertFrom-Json $input
# Check that the state member equals "Running"
$result = $json.state -eq "Running"
# Return the result
$result
You should do some check that the state
member exists in $json
.
Upvotes: 3
Reputation: 977
If you really want to use regex, you could try this:
(?<="state":) ?"(\w*?)"
https://regex101.com/r/W25veP/3
It will capture the value of state in the first capturing group.
But I think there should be some constructs that allow you parse JSON in the language of your choice. That would in the most of the cases the best choice.
Basically you will capture "Running". I assume you want to map it to "True".
Upvotes: 2