1

I'm trying to solve a problem for a customer. I don't have access to the same software as they do. So, I've created this demonstration code.

What I would like to suggest is for them to consume JSON. But, what I found for ConvertFrom-Json on PowerShell 5.1 is that it is emitting a single array as output.

They want to use Where-Object. But, in my testing, that will return the whole array.

Normally, I would expect PowerShell to emit single objects. I'm not sure why I'm getting one object (the entire array).

I noticed if I shut down the pipeline (Example One) then I get the behavior I want.

I'm aware of where(). But, I want to take advantage of the customer's muscle memory--<# objects #> | Where-Object { <# test #> }--and use the pipeline.

This also seemed like it could be improved: | ForEach-Object { $_.ForEach({ $_ }) } |


Question

Is there a better way to emit individual objects without shutting down the pipeline?


> # Example One
> (& '.\Stack Overflow Demo 03.ps1' | ConvertTo-JSON | ConvertFrom-Json) | Select-Object -First 1


Id                        : ba7fab67-5cd7-401a-9c88-c4a2e88f6932
Name                      : Development
Description               : Default development device group
ProductId                 : 556957b4-aa39-4e5a-aa4c-d3208c277f3c
OsFeedType                : Retail
UpdatePolicy              : Accept only system software updates. Don't accep
AllowCrashDumpsCollection : False
CurrentDeployment         : None


> # Example Two
> & '.\Stack Overflow Demo 03.ps1' | ConvertTo-JSON | ConvertFrom-Json | Select-Object -First 1


Id                        : ba7fab67-5cd7-401a-9c88-c4a2e88f6932
Name                      : Development
Description               : Default development device group
ProductId                 : 556957b4-aa39-4e5a-aa4c-d3208c277f3c
OsFeedType                : Retail
UpdatePolicy              : Accept only system software updates. Don't accep
AllowCrashDumpsCollection : False
CurrentDeployment         : None

Id                        : 45acb705-a591-4e41-ba1e-4b3dd062e1ec
Name                      : Field Test
Description               : FieldTest
ProductId                 : 556957b4-aa39-4e5a-aa4c-d3208c277f3c
OsFeedType                : Retail
UpdatePolicy              : Accept all updates from the Azure Sphere Securit
AllowCrashDumpsCollection : False
CurrentDeployment         : d684d491-9229-484f-a2eb-fda0168f7e27

...
  • 1
    This is a known "behaviour" in PowerShell 5.1 - see https://github.com/PowerShell/PowerShell/issues/3424. One additional workaround suggested there is to use pipe ```ConvertFrom-Json``` into ```Write-Output``` to force enumeration of the array items. – mclayton Jun 25 '21 at 19:28
  • Before PowerShell (Core) 7.0, `ConvertFrom-Json` and `Invoke-RestMethod` unexpectedly sent arrays as _a whole_ through the pipeline, rather than element by element. The simplest workaround is to enclose the call in `(...)`, which forces enumeration. See [this answer](https://stackoverflow.com/a/61412070/45375) to the linked duplicate. – mklement0 Jun 25 '21 at 20:50

0 Answers0