'Why does powershell ConvertFrom-Json not work when converting json array and streaming output to ForEach-Object
This JSON array converted to powershell object seems to be somehow get handled as single string when streamed to ForEach-Object.
"[1, 2, 3]" | ConvertFrom-Json | ForEach-Object {Write-Host "Number: $_"}
Prints:
Number: 1 2 3
Expecting:
Number: 1
Number: 2
Number: 3
Although: "[1, 2, 3]" | ConvertFrom-Json
prints:
1
2
3
I was of course trying to do something bit more complex with Azure CLI, but this was the problem that wasted me quite some time.
And actually setting parenthesis like this seems to work as I expect:
("[1, 2, 3]" | ConvertFrom-Json) | ForEach-Object {Write-Host "Number: $_"}
And outputs:
Number: 1
Number: 2
Number: 3
Is this a piping issue in powershell or do I just not understand piping in powershell.
My powershell version is as follows:
> $PSversionTable
Name Value
---- -----
PSVersion 5.1.22000.282
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22000.282
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
