ConvertTo-Json behaviour with array with single element inconsistent when piping
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Single element
PS> $arr = @(1)
PS> ConvertTo-Json -InputObject $arr -Compress # [1]
PS> $arr | ConvertTo-Json -Compress # 1
Multiple elements
PS> $arr = @(1,2)
PS> ConvertTo-Json -InputObject $arr -Compress # [1,2]
PS> $arr | ConvertTo-Json -Compress # [1,2]
Expected behavior
I expected the same behaviour from both of these lines when an array contains one item:
PS> $arr = @(1)
PS> ConvertTo-Json -InputObject $arr -Compress # [1]
PS> $arr | ConvertTo-Json -Compress # [1]
Actual behavior
ConvertTo-Json
with piped input converts a single element array to a string without array characters ([]
).
Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.2.6
PSEdition Core
GitCommitId 7.2.6
OS Darwin 21.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:19:52 PDT 2022; root:xnu-8020.140.49~2/RELEASE_ARM64_T6000
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6
Top Results From Across the Web
Strange behavior when piping converted JSON into ForEach
The issue is that ConvertFrom-Json wraps the JSON array in an array and then passes the whole array down the pipeline as one...
Read more >Powershell - Creating a JSON array of one element
Solution: Looks like it won't work if you pipe the object to ConvertTo-Json. You need to specify the object with -InputObject.
Read more >about Arrays - PowerShell
An array is a data structure that's designed to store a collection of items. The items can be the same type or different...
Read more >Xslt replace single quote. Oxygen XML Developer. Searching ...
All Caps with Commands Slight inconsistency between probability estimated from ... Warning. whitespace within element tags, and the choice of single or ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is quite error-prone behavior of
ConvertTo-Json
for single element arrays, I wish it was fixed.@jhoneill makes sense when you explain it.
So this is a workaround I guess?
I will either way restort to using
InputObject
in the future because this confused me for a fair while.Still seems a bit inconsistent that multiple elements yields proper output when piping.