You can use the hidden psobject
property to obtain the individual property names on the $response2.results.daily
object, then use those to access each array:
# use psobject to enumerate the property names (ie. '5/10/2020')
$dates = $response2.results.daily.psobject.Properties.Name
# loop through the property names
$dailyStats = $dates |ForEach-Object {
$date = $_
# Add the name (= date) as a new "date" property to the object
$response2.results.daily.$name |Select-Object @{Name='date';Expression={$name}},*
}
$dailyStats
will now be an array of objects like:
date total sms-in sms-out suppress
---- ----- ------ ------- --------
5/10/2020 $31.770 102 2,016 1
5/9/2020 $0.060 4 0 0
etc...
Is there a better way to handle APIs through PS?
No idea, given that we don't know how you obtained the response in the first place ¯\_(ツ)_/¯