Arrays within Arrays - How to keep my output simple?


This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Daniel Watson Daniel Watson 2 days, 14 hours ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
  • #55324
    Profile photo of Daniel Watson
    Daniel Watson
    $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?

    Profile photo of Rob Simmers
    Rob Simmers

    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
    Profile photo of Daniel Watson
    Daniel Watson

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

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.