1

I've made an API request to the ManageEngine tech support database which returns a JSON response which is returned as a custom object of three hash tables: $result.response_status, $result.list_info, and $result.requests The third of these contains the data I'm looking for.

$results.requests.id an integer

$results.requests.subject a string

$results.requester a hashtable.

The requester hash table is @requester{name= 'Martin Zimmerman'; email='mzimmer@company.com'}

What I'd like to be able to do is:

$results.requests | select id, subject, requester.name  

to get a single line displaying the id, subject and requester's name

id   subject         name
--   --------------  --------------
3329 Can't open file Martin Zimmerman 

However, I cannot figure out the nomenclature to extract the value of the name key in the requester hash table.

pppery
  • 3,731
  • 22
  • 33
  • 46
user2901305
  • 61
  • 1
  • 1
  • 3
  • 1
    If you've parsed JSON via `ConvertFrom-Json`, note that what you get are (potentially nested) `[pscustomobject]` instances, not hashtables. _Stringified_ `[pscustomobject]` instances _look somewhat like_ hashtable literals, which can cause confusion - see [this answer](https://stackoverflow.com/a/53107600/45375). – mklement0 May 29 '20 at 19:26

1 Answers1

2

I think this is what you want. You need to build a Calculated Property (Example 10).

$results.requests | Select-Object id,subject,@{l='name';e={$_.requester['name']}}

This approach allows you to call the name key from the hashtable.

EDIT

If your requester item is a PSCustomObject, then try this.

$results.requests | Select-Object id,subject,@{l='name';e={$_.requester.name}}

Test with similar object structure.

$results = New-Object psobject
$requests = New-Object psobject
$requests | Add-Member -MemberType NoteProperty -Name id -Value 2
$requests | Add-Member -MemberType NoteProperty -Name subject -Value "lol"
$hash = @{}
$hash.Add("email","mzimmerman@company.com")
$hash.Add("name","Martin Zimmerman")
$requests | Add-Member -MemberType NoteProperty -Name requester -Value $hash
$results | Add-Member -MemberType NoteProperty -Name requests -Value $requests
$results.requests | select id,subject,@{l='name';e={$_.requester['name']}}

id subject name
-- ------- ----
 2 lol     Martin Zimmerman
Ash
  • 3,030
  • 3
  • 15
  • 33
  • Hm....tried that but it came back with nothing for the name. I don't know if it makes any difference, but if I do a Get-Member on the $results.requester, it says: requester NoteProperty System.Management.Automation.PSCustomObject requester=@{email_id=tmzimm@company.com; name= Martin Zimmerman} – user2901305 May 29 '20 at 19:01
  • 1
    If I have understood your data structure properly, my answer works and the test is only added to prove it. If under `$results.requests` you have a property called `requester` that contains a hashtable with a key called `name`, you just need to use the one line answer I have posted. If your data structure is different, then you need to update your question by showing what you see in the console if you print `$results.requests`. It sounds that you haven't actually got a hashtable... – Ash May 29 '20 at 19:20
  • 1
    Try `$_.requester.Name` instead, which is required to access a `[pscustomobject]` instance's property (the syntax also works with hashtables); what you, @user2901305, saw may have _looked_ like a hashtable to you, but it's merely how `[pscustomobject]` instances _stringify_. – mklement0 May 29 '20 at 19:21
  • Ash... Many thanks for your replies, indeed the first one-liner worked correctly with the calculated property, after I started over again. `$results.requests | Select-Object id,subject,@{l='name';e={$_.requester.name}}` I think my problem may have been either: I ended up storing the output from the API call as a string, or, I was referencing a different variable. In short....it was a really dumb mistake on my part. Of interest is the whole notion of a calculated property...something I hadn't run across before. Many thanks for your help! – user2901305 Jun 01 '20 at 14:15