Home > OS >  How to filter JSON file from Invoke-RestMethod in PowerShell
How to filter JSON file from Invoke-RestMethod in PowerShell

Time:01-10

Tried to lookup this, but do not quite understand how to get this done. What I need to be able to do is to filter out the following:

$response = Invoke-RestMethod 'cannot share of course' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

with the response of (cut out and masked some parts here, for a reason):

{
        "url":  "***",
        "id":  ***,
        "guid":  "***",
        "name":  "***",
        "check_type":  "***",
        "check_type_name":  "Real Browser, Chrome",
        "check_type_api":  "browser",
        "enabled":  true,
        "location":  "***",
                                },
        "tags":  {
                     "Country":  "***",
                     "ITSystemCode":  "***"
                 }
    }

I am able to get the content, I want to be able pull out 'id' and 'name', and if it's 'enabled' true or false. Tried with Where-Object but seems it is the wrong way of doing it?

Much appreciated in advance!

CodePudding user response:

Thanks for the tips. I will answer this as this did help me with my task.

What I did was I added "$response | Select id,name" after the second line which Selects Id and Name from the requested content/JSON and displays only Id and Name values, as well as for the extra I did export the list to a .csv file using Out-File "C:\Code\checks.csv" on the line 3.

$response = Invoke-RestMethod '***' -Method 'GET' -Headers $headers
$response | ConvertTo-Json
$response | Select id,name | Out-File "C:\Code\checks.csv"
  •  Tags:  
  • Related