I'm doing a simple API call to a government database. I am interested in the address field. Although it looks like a hash, I can't get the values.
Here is the API call:
$searchTerm = "01446754"
$url = "https://api.companieshouse.gov.uk/search?q=$searchTerm"
$apiKey = "myregisteredapikey"
$authVal = "Basic " + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($apikey))
$header = @{
"Authorization"=$authVal
}
$response = Invoke-RestMethod -Uri $url -Headers $header -Method Get -ContentType 'application/json'
$results = $response.items
$results
In this example, searching by company number, I get the following dataset returned:
links : @{self=/company/01446754}
description_identifier : {incorporated-on}
kind : searchresults#company
company_status : active
address : @{premises=Lakeside Works Rocester; postal_code=ST14 5JP; locality=Staffordshire; address_line_1=Uttoxeter}
date_of_creation : 1979-09-05
address_snippet : Lakeside Works Rocester, Uttoxeter, Staffordshire, ST14 5JP
description : 01446754 - Incorporated on 5 September 1979
company_type : ltd
company_number : 01446754
title : JCB LIMITED
All good, until I do $results.Address which returns:
OverloadDefinitions
-------------------
System.Object&, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Address(int )
I am not sure what this is telling me. Intellisense adds a bracket to the end; (which, I think) suggests it is an object, so I tried:
PS C:\> $results.Address()
Cannot find an overload for "Address" and the argument count: "0".
At line:1 char:1
+ $results.Address()
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest
Clearly, I don't know what I am doing here, but I tried to use one of the fields returned in the 'address' hash:
PS C:\> $results.Address('postal_code')
Cannot convert argument "0", with value: "postal_code", for "Address" to type "System.Int32": "Cannot convert value "postal_code" to type "System.Int32". Error: "Input
string was not in a correct format.""
At line:1 char:1
+ $results.Address('postal_code')
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument
So it seems to want an integer, so I tried:
PS C:\> $results.Address(1)
Exception calling "Address" with "1" argument(s): "Operation could destabilize the runtime."
At line:1 char:1
+ $results.Address(1)
+ ~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VerificationException
Ok, lets look at what type 'address' is:
PS C:\> $results.Address.gettype()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True False PSMethod System.Management.Automation.PSMethodInfo
So how do I get the values out of the object of type PSMethod?