1

I am able to get data back via Invoke-RestMethod from an API which provides the below format:

    1612142668000000000 : @{peak_performance=29.18; current_utilization=19.15}
    1612146268000000000 : @{peak_performance=29.05; current_utilization=20.03}
    1612149868000000000 : @{peak_performance=29.07; current_utilization=18.86}

If it helps, the JSON format of the above is:

{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}

I would like to be able to calculate and display the averages for all of the peak-performance and current_utilization values available, but I can't figure out how to do this. Any ideas?

Dustjunky
  • 13
  • 2
  • 1
    it's a little clunky because the API returns a nested object instead of an array; this means you have to iterate over the properties. Something like `($response.PSObject.Properties.Value | Measure-Object -Property current_utilization,peak_performance -Average`, with further processing as necessary. – Jeroen Mostert May 21 '21 at 09:54

1 Answers1

0

there are lot of solutions:

$json = @"
{
  "1612142668000000000": {
    "peak_performance": 29.18,
    "current_utilization": 19.15
  },
  "1612146268000000000": {
    "peak_performance": 29.05,
    "current_utilization": 20.03
  },
  "1612149868000000000": {
    "peak_performance": 29.07,
    "current_utilization": 18.86
  }
}
"@

$jout = $json | ConvertFrom-Json
$result = $jout.PSObject.Properties.Value | Measure-Object -Property current_utilization,peak_performance -Average

foreach($v in $result){
    Write-Host $v.Property   " = "  $v.Average
}

result:

current_utilization  =  19.3466666666667
peak_performance  =  29.1
Frenchy
  • 16,386
  • 3
  • 16
  • 39