Skip to content

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

Description

Based on a suggestion by @AikenBM, welcomed by Steve Lee (@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

Activity

  1. changed the title [-]Add switch `-AsArray` to ConvertTo-Json to support unconditional conversion as an array[/-] [+]Add an -AsArray switch to ConvertTo-Json to support unconditional conversion to a JSON array[/+] on Mar 6, 2018
  2. added
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Mar 8, 2018
  3. kalgiz commented on Mar 20, 2018

    @kalgiz
    Contributor

    Fix proposal in #6438.

  4. removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Mar 20, 2018
  5. kalgiz commented on Mar 23, 2018

    @kalgiz
    Contributor

    Fixed in #6438.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions