Forum Discussion
Ronald Lawrimore
Jul 27, 2021Copper Contributor
Powershell JSON
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 furt...
Ronald Lawrimore
Jul 28, 2021Copper Contributor
Here is a link to some sample data in the form a JSON.
https://1drv.ms/u/s!AkRm_j0CiExil9814YShSslGv9IIJA?e=hlNllK
gerald_doeserich
Jul 28, 2021Copper Contributor
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
}