Powershell JSON

%3CLINGO-SUB%20id%3D%22lingo-sub-2592591%22%20slang%3D%22en-US%22%3EPowershell%20JSON%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2592591%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20pulling%20back%20some%20JSON%20formatted%20data%20via%26nbsp%3B%20Invoke-RestMethod.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20am%20trying%20to%20figure%20out%20how%20to%20get%20that%20data%20into%20usable%20format%20so%20that%20I%20can%20use%20the%20returned%20data%20in%20If%20else%20statements%20further%20down%20in%20the%20script%20I%20am%20using.%20The%20data%20is%20formatted%20such%20as%20when%20I%20use%20the%20following%20commands.%3C%2FP%3E%3CP%3E%24searchResultsResponse%20%3D%20Invoke-RestMethod%20-Method%20POST%20-Uri%20%24searchUri%20-ContentType%20application%2Fjson%20-Header%20%24requestHeader%20-Body%20%24searchResultsRequest%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%24searchResultsResponse.businessObjects.fields%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDisplayName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BValue%3C%2FP%3E%3CP%3EIncident%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%201%3C%2FP%3E%3CP%3ELocation%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20NC%3C%2FP%3E%3CP%3EStatus%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%204%3C%2FP%3E%3CP%3EIncident%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B2%3C%2FP%3E%3CP%3ELocation%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20NC%3C%2FP%3E%3CP%3EStatus%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B2%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2592591%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Contributor

I am pulling back some JSON formatted data via  Invoke-RestMethod.

 

I am trying to figure out how to get that data into usable format so that I can use the returned data in If else statements further down in the script I am using. The data is formatted such as when I use the following commands.

$searchResultsResponse = Invoke-RestMethod -Method POST -Uri $searchUri -ContentType application/json -Header $requestHeader -Body $searchResultsRequest

 

$searchResultsResponse.businessObjects.fields

 

DisplayName     Value

Incident                1

Location                NC

Status                    4

Incident                 2

Location                NC

Status                     2

3 Replies

Without seeing the actual JSON layout and what you want to use in the If clause we can't really help you.
But Invoke-RestMethod should automatically convert any JSON to a PSCustomObject which you should be able to use in any If/Else clause.

Here is a link to some sample data in the form a JSON. 

JSON File 

@gerald_doeserich 

@Ronald Lawrimore 

I don't think there is a direct way of parsing the inner fields to a PSCustomObject.

The following should do the trick and give you a PSCustomObject to work with:

# $JSON is the result from your Invoke-WebRequest
ForEach($entry in $JSON)
{
    $newFields = @()
    $rawFields = $entry.Fields
    ForEach($rawField in $rawFields)
    {
        $parameters = [PSCustomObject]@{}

        # Remove first 2 letters (as they are '@{') and the last '}'
        $rawField = $rawField.Substring(2, $rawField.Length - 4)

        # Parameters seem to be splitted by a ';'
        $rawParameters = $rawField -split ';'
        ForEach($pair in $rawParameters)
        {
            # Key = Value
            $split = $pair -split '='
            # Trim the start as after the ';' a space occurs
            $name = $split[0].TrimStart()
            # Skip empty pairs
            If([String]::IsNullOrEmpty($name))
            {
                Continue
            }
            $parameters | Add-Member -NotePropertyName $name -NotePropertyValue $split[1]
        }

        $newFields += $parameters
    }

    # Override original Fields entry
    $entry.Fields = $newFields
}