Powershell - How to retrieve Object[] data

257 views Asked by At

I'm trying to get some data from api request, but rank it's getting corrupted. I have this code in a file:

$queryResult=Invoke-restmethod -Uri "https://aoe2.net/api/leaderboard?game=aoe2de&leaderboard_id=3&steam_id=76561198011209758"

Write-Output "COMPLETE RETURN"
$queryResult

Write-Output "LEADERBOARD"
$queryResult.leaderboard

Write-Output "INDIVIDUAL ITEMS"
Write-Output $("profile_id: "+$queryResult.leaderboard.profile_id)
Write-Output $("rank: "+$queryResult.leaderboard.rank)
Write-Output $("rating: "+$queryResult.leaderboard.rating)
Write-Output $("steam_id: "+$queryResult.leaderboard.steam_id)
Write-Output $("icon: "+$queryResult.leaderboard.icon)
Write-Output $("name: "+$queryResult.leaderboard.name)
Write-Output $("clan: "+$queryResult.leaderboard.clan)
Write-Output $("country: "+$queryResult.leaderboard.country)
Write-Output $("previous_rating: "+$queryResult.leaderboard.previous_rating)
Write-Output $("highest_rating: "+$queryResult.leaderboard.highest_rating)
Write-Output $("streak: "+$queryResult.leaderboard.streak)
Write-Output $("lowest_streak: "+$queryResult.leaderboard.lowest_streak)
Write-Output $("highest_streak: "+$queryResult.leaderboard.highest_streak)
Write-Output $("games: "+$queryResult.leaderboard.games)
Write-Output $("wins: "+$queryResult.leaderboard.wins)
Write-Output $("losses: "+$queryResult.leaderboard.losses)
Write-Output $("drops: "+$queryResult.leaderboard.drops)
Write-Output $("last_match: "+$queryResult.leaderboard.last_match)
Write-Output $("last_match_time: "+$queryResult.leaderboard.last_match_time)

Whenever i ran it, it shows:

COMPLETE RETURN


total          : 50403
leaderboard_id : 3
start          : 1
count          : 1
leaderboard    : {@{profile_id=518981; rank=4690; rating=1414; steam_id=76561198011209758; icon=; name=[LdS]Nestoter; clan=LDS14; country=UY; previous_rating=1398; highest_rating=1432; streak=1; lowest_streak=-10; highest_streak=8;
                 games=141; wins=80; losses=61; drops=1; last_match=1619919442; last_match_time=1619919442}}

LEADERBOARD
profile_id      : 518981
rank            : 4690
rating          : 1414
steam_id        : 76561198011209758
icon            :
name            : [LdS]Nestoter
clan            : LDS14
country         : UY
previous_rating : 1398
highest_rating  : 1432
streak          : 1
lowest_streak   : -10
highest_streak  : 8
games           : 141
wins            : 80
losses          : 61
drops           : 1
last_match      : 1619919442
last_match_time : 1619919442

INDIVIDUAL ITEMS
profile_id: 518981
rank: 1
rating: 1414
steam_id: 76561198011209758
icon:
name: [LdS]Nestoter
clan: LDS14
country: UY
previous_rating: 1398
highest_rating: 1432
streak: 1
lowest_streak: -10
highest_streak: 8
games: 141
wins: 80
losses: 61
drops: 1
last_match: 1619919442
last_match_time: 1619919442

I don't understand why rank changes from 4690 to 1, when I print the variable $queryResult and $queryResult.leaderboard contents it clearly shows that rank=4690, but when i access the innermost element it prints rank: 1.

Why rank changes from 4690 to 1?

You can copy the code and try yourself, the API is public.

Thanks

1

There are 1 answers

0
mklement0 On BEST ANSWER

iRon has provided the crucial pointer: Because $queryResult.leaderboard is an array, which itself has a .Rank property, you must explicitly access the element(s) of interest to get their .rank property value (note that property access is case-insensitive in PowerShell, as PowerShell generally is):

# Note the [0] for access to the 1st element.
$queryResult.leaderboard[0].rank # -> 4690

You can determine that $queryResult.leaderboard is an array with $queryResult.leaderboard | Get-Member or $queryResult.leaderboard.GetType().Name, or you can infer it from the for-display representation: the outer {...} in {@{profile_id=518981; ..}} indicates that the value is a collection (though not necessarily an array).

The reason this explicit element access ([0]) isn't necessary for your other properties is due to a feature called member-access enumeration:

  • It conveniently allows you to use property access on a collection in order to have the property values of its elements returned:

    • With a multi-element collection, the elements' property values are returned as an array ([object[]]), but - as in the pipeline - with a single-element collection, the only element's property value is returned as-is.

    • Therefore, for instance, given an object $obj = [pscustomobject] @{ profile_id = 518981 }:

      • Single-element case: @($obj).profile_id is effectively the same as:
        @($obj)[0].profile_id / $obj.profile_id
      • Multi-element case: @($obj, $obj).profile_id is effectively the same as:
        $obj | ForEach-Object { $_.profile_id }
  • However, if the collection itself has a property by the given name, that collection-level property takes precedence - which is what happened with .Rank.

    • GitHub issue #7445 discusses this situational ambiguity and proposes introducing a dedicated operator, say %. so that you can unambiguously request either regular property access or member-access enumeration.

    • As a general workaround that is faster than the use of ForEach-Object or Select-Object, you can use .ForEach('rank'), as discussed in this answer.