Arrays within Arrays - How to keep my output simple?

Welcome Forums General PowerShell Q&A Arrays within Arrays - How to keep my output simple?

This topic contains 2 replies, has 2 voices, and was last updated by

2 years, 3 months ago.

  • Author
  • #55324

    Points: 0
    Rank: Member
    $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
    $headers.Add("Accept-Tenant", 'uk')
    $headers.Add("Accept-Language", 'en-gb')
    $headers.Add("Authorization", 'Basic VGVjaFRlc3RBUEk6dXNlcjI=')
    $headers.Add("host", '')
    Invoke-RestMethod -Uri -Method 'Get' -Headers $headers | `
    Select-Object -ExpandProperty Restaurants | Format-List -Property Name,CuisineTypes,RatingAverage

    I've figured out this little script, I need the three Properties at the end, but one of them is just another array!

    Name          : Taste Haven
    CuisineTypes  : {@{Id=97; Name=African; SeoName=african}, @{Id=66; Name=Nigerian; SeoName=nigerian}}
    RatingAverage : 5.33
    Name          : Morley's
    CuisineTypes  : {@{Id=79; Name=Chicken; SeoName=chicken}, @{Id=78; Name=Burgers; SeoName=burgers}}
    RatingAverage : 3.06

    As far as my understanding goes right now, I'll have to Select-Object again to drill down to cuisine type, but that will get rid of everything else.

    Is there a way to select just the names within CuisineTypes without ditching everything else?

  • #55325

    Points: 438
    Helping Hand
    Rank: Contributor

    The headers can also be defined as a hashtable. This is a way to extract the Name of the Cuisine to show as an array like so:

    $headers = @{}
    $headers.Add("Accept-Tenant", 'uk')
    $headers.Add("Accept-Language", 'en-gb')
    $headers.Add("Authorization", 'Basic VGVjaFRlc3RBUEk6dXNlcjI=')
    $headers.Add("host", '')
    $restaurants = Invoke-RestMethod -Uri -Method 'Get' -Headers $headers | Select-Object -ExpandProperty Restaurants 
    $restaurants | Select Name, @{Name="CuisineTypes";Expression={$_.CuisineTypes | Select -ExpandProperty Name}}, RatingAverage


    Name                                         CuisineTypes               RatingAverage
    ----                                         ------------               -------------
    Napoli Pizza                                 {Italian, Pizza}                    5.02
    Yak & Yeti                                   {Indian, Curry}                     5.06
    Palace Spice                                 Indian                              5.26
    Godfather Pizza Wood Oven                    {Italian, Pizza}                    4.57
    Moorish Grill                                {Jamaican, Caribbean}               4.08
    Golden Curry                                 {Bangladeshi, Indian}               5.41
    Monkeys                                      Indian                              5.28
    Eastern Cuisine Indian Restaurant & Takeaway Indian                              5.36
    Gurkha Cottage                               {Indian, Nepalese}                  5.04
    Shanghai Wok                                 {Chinese, Curry}                    5.31
  • #55332

    Points: 0
    Rank: Member

    That's great thanks! I didn't think about making a new hash table, Russian Doll style.

The topic ‘Arrays within Arrays - How to keep my output simple?’ is closed to new replies.