Arrays within Arrays - How to keep my output simple?

This topic contains 2 replies, has 2 voices, and was last updated by  Daniel Watson 1 year, 1 month ago.

  • Author
    Posts
  • #55324

    Daniel Watson
    Participant
    $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", 'public.je-apis.com')
    
    
    Invoke-RestMethod -Uri https://public.je-apis.com/restaurants?q=se19 -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

    Rob Simmers
    Participant

    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", 'public.je-apis.com')
    
    $restaurants = Invoke-RestMethod -Uri https://public.je-apis.com/restaurants?q=se19 -Method 'Get' -Headers $headers | Select-Object -ExpandProperty Restaurants 
    $restaurants | Select Name, @{Name="CuisineTypes";Expression={$_.CuisineTypes | Select -ExpandProperty Name}}, RatingAverage
    

    Output:

    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

    Daniel Watson
    Participant

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

You must be logged in to reply to this topic.