How to parse JSON from the Invoke-WebRequest in PowerShell?

Solution 1:

You could replace Invoke-WebRequest with Invoke-RestMethod which auto-converts json response to a psobject so you can use:

$response = Invoke-RestMethod -Uri "https://yadayada:8080/bla"
$response.flag 

Solution 2:

If you have a need to use Invoke-WebRequest over Invoke-RestMethod you can convert it to an object by turning it into a string first

$response = Invoke-WebRequest -Uri "https://yadayada:8080/bla"
$jsonObj = ConvertFrom-Json $([String]::new($response.Content))

Solution 3:

This way:

$response = Invoke-WebRequest -Uri <your_uri>
if ($response.statuscode -eq '200') {
    $keyValue= ConvertFrom-Json $response.Content | Select-Object -expand "<your_key_name>"
}