Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConvertFrom-Json output doesn't work for Select-Object

I am using PowerShell 4 to make a series of web requests. From one call I get a generic array - for the sake of discussion it looks like this

$data = '[{"Id":"1","Name":"One"},{"Id":"2","Name":"Two"}]'

I am trying to parse this data to pull out the Name properties. However, when I use the following call it writes a line with Name and no information under it:

$data | ConvertFrom-Json | Select-Object Name

But if I save the object to an intermediate object like this it works:

$o1 = $data | ConvertFrom-Json
$o1 | Select-Object Name

I get the proper output.

The object types are different but I don't understand why. Here's output from relevant Get-Member calls:

$test | ConvrtFrom-Json | gm

TypeName: System.Object[]

and

$o1 | gm

TypeName: System.Management.Automation.PSCustomObject

Can anyone help me understand what I'm doing wrong in terms of my collection management? I'd like to be able to do this in one statement.

like image 928
Corez Avatar asked Sep 13 '25 09:09

Corez


1 Answers

It seems like the parentheses are needed for some mysterious reason, as OP pointed out in the comment. Adding parentheses was the fix for me as well. I am not sure why this breaks without parens, but I can confirm that code without parens is not an issue when executed in powershell core.

like image 57
Negatar Avatar answered Sep 14 '25 23:09

Negatar