Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add an -AsArray switch to ConvertTo-Json to support unconditional conversion to a JSON array #6327

Copy link
Copy link
@mklement0

Description

@mklement0
Issue body actions

Based on a suggestion by @AikenBM, welcomed by @SteveL-MSFT:

Similar to how @(...) ensures treatment of any value as an array, it would be handy if an -AsArray switch could instruct ConvertTo-Json to treat its input as an [ultimately JSON] array, so you can guarantee that your pipeline input is treated as an array, even if it happens to be a single item only:

Wishful thinking:

> $in = 1; $in | ConvertTo-Json -AsArray
[ 
  1 
] 
# scalar 1 was treated as an array

> $in = 1, 2; $in | ConvertTo-Json -AsArray
[ 
  1, 
  2 
]  
# Input that already is an array is left as-is

Awkward workaround, in the absence of -AsArray:

> $in = 1; , @($in) | ConvertTo-Json -AsArray
[ 
  1 
] 

Environment data

Written as of PowerShell Core v6.0.1

Reactions are currently unavailable

Metadata

Metadata

Assignees

Labels

Issue-Enhancementthe issue is more of a feature request than a bugthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.The issue is fixed.WG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility modulecmdlets in the Microsoft.PowerShell.Utility module

Type

No type
No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.