0

I'm a bit stuck. Why this pipeline returns nothing

Get-Content -Path C:\file.json | ConvertFrom-Json | Where-Object -Property FullName -Like "*somefilter*"

But when I split in into two pipelines it acually returns what I want - content of the json file (which is an array of objects) filtered by FullName

$t = Get-Content -Path C:\file.json | ConvertFrom-Json
$t | Where-Object -Property FullName -Like "*somefilter*"
Mak Sim
  • 2,148
  • 19
  • 30
  • Looks similar to [ConvertFrom-Json pipeline mystery](https://stackoverflow.com/questions/42842781). Could you post a JSON file example? – beatcracker Nov 29 '21 at 09:20
  • `Get-Content -Path C:\file.json |ConvertFrom-Json |Write-Output |Where-Object ...`, see: [`#3424` ConvertFrom-Json sends objects converted from a JSON array as an *array* through the pipeline.](https://github.com/PowerShell/PowerShell/issues/3424) – iRon Nov 29 '21 at 09:35
  • Yes. Thanks. It is definitely a duplicate. Just didn't figured out how to google it. – Mak Sim Nov 29 '21 at 10:01

1 Answers1

0

This is a know issue which is described here: #3424 ConvertFrom-Json sends objects converted from a JSON array as an array through the pipeline. and Powershell calling Github API: ConvertFrom-Json pipeline mystery:

$Json = @'
[
  {
    "FullName": "a"
  },
  {
    "FullName": "b"
  },
  {
    "FullName": "c"
  }
]
'@

Issue

$Json |ConvertFrom-Json |Where-Object -Property FullName -Like 'a'

Returns nothing because in the older version of ConvertFrom-Json sends an array (as a single item) through the pipeline rather separate items:

($Json |ConvertFrom-Json).Count # returns 1 (in older versions of PowerShell)

As a workaround, you might simply unroll the pipeline items using Write-Output:

$Json |ConvertFrom-Json |Write-Output |Where-Object -Property FullName -Like 'a'

FullName
--------
a

The issue is resolved in newer PowerShell (core 7.x) versions.

iRon
  • 20,463
  • 10
  • 53
  • 79