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='[email protected]'}

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.

1
  • 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. Commented May 29, 2020 at 19:26

1 Answer 1

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","[email protected]")
$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
Sign up to request clarification or add additional context in comments.

4 Comments

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 protected]; name= Martin Zimmerman}
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...
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.
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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.