question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ConvertTo-Json behaviour with array with single element inconsistent when piping

See original GitHub issue

Prerequisites

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

image

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

5reactions
nosalancommented, Mar 3, 2023

This is quite error-prone behavior of ConvertTo-Json for single element arrays, I wish it was fixed.

3reactions
johanredeploycommented, Oct 3, 2022

@jhoneill makes sense when you explain it.

So this is a workaround I guess?

$arr = @(1)
,$arr | ConvertTo-Json -Compress # [1]

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found