1

Data

Data for the example:

$json = @"
[
    {
        "DATE":  "2022-01-05",
        "WALCL":  "8765721.0"
    },
    {
        "DATE":  "2022-01-12",
        "WALCL":  "8788278.0"
    },
    {
        "DATE":  "2022-01-19",
        "WALCL":  "8867834.0"
    },
    {
        "DATE":  "2022-01-26",
        "WALCL":  "8860485.0"
    },
    {
        "DATE":  "2022-02-02",
        "WALCL":  "8873211.0"
    }
]
"@    

Example : works as expected

Display a single item:

$cde = $json | ConvertFrom-Json

$cde | Select-Object -First 1

Result:

DATE       WALCL
----       -----
2022-01-05 8765721.0

OK, this works as expected.

Example : different behavior

Similar code without the temporary variable:

$json | ConvertFrom-Json | Select-Object -First 1

It does not display a single line this time, but all of them:

DATE       WALCL
----       -----
2022-01-05 8765721.0
2022-01-12 8788278.0
2022-01-19 8867834.0
2022-01-26 8860485.0
2022-02-02 8873211.0

Question

Why does the version without the temporary variable return multiple lines despite the Select-Object -First 1?

dharmatech
  • 8,979
  • 8
  • 42
  • 88
  • 2
    It seems that you're using Windows PowerShell 5.1, in which case, `ConvertFrom-Json` output __is not enumerated__ by default thus the behavior you're observing. You can work around this by forcing enumeration by wrapping the expression with `(...)`: `($json | ConvertFrom-Json) | Select-Object -First 1`. In PowerShell 7+ you wouldnt have such problem – Santiago Squarzon Jun 09 '23 at 16:03

0 Answers0