Scott Lezberg Scott Lezberg - 3 months ago 20
Git Question

Powershell - Getting multiple values from a nested object

I am sure this is a very basic PowerShell question but I have a JSON file that I am loading into a PowerShell custom object with

ConvertFrom-Json
and the following
select-object
works

$JsonContent.value | Select-Object -Property Name, @{Name = 'commit'; Expression = { $_.commit.author.date }}


I am trying to get another column in the nested commit object and the following is NOT working so am hoping someone can help me figure out how to get a second value from the nested commit object (I have tried a few variations but not working)

JsonContent.value | Select-Object -Property Name, @{Name = 'commit'; Expression = { $_.commit.author.date}, {$_.commit.commitId}}


Thanks

Scott

Answer

The part after Expression = ... is a script block containing code so if you want to return an array of values there for being the value for the property commit then just modify

Expression = { $_.commit.author.date}, {$_.commit.commitId}  

to

Expression = { $_.commit.author.date, $_.commit.commitId}

so that you use the commma operator to create and return your 2-element array


Probably you rather want to make some nice string representing the commit e.g.

Expression = { "{0} - {1}" -f $_.commit.author.date, $_.commit.commitId }

In case you want the commitId in a completely different column just add another property like so

... -Property Name, @{Name = 'commit'; Expression = { $_.commit.author.date}}, @{Name = 'commitId'; Expression = {$_.commit.commitId}} 
Comments