Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mklement0 opened this issue Mar 6, 2018 · 2 comments

Comments

@mklement0
Copy link
Contributor

@mklement0 mklement0 commented Mar 6, 2018

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

@mklement0 mklement0 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 Mar 6, 2018
@SteveL-MSFT SteveL-MSFT added this to the 6.1.0-Consider milestone Mar 8, 2018
@kalgiz kalgiz self-assigned this Mar 19, 2018
@kalgiz

This comment has been minimized.

Copy link
Contributor

@kalgiz kalgiz commented Mar 20, 2018

Fix proposal in #6438.

@kalgiz

This comment has been minimized.

Copy link
Contributor

@kalgiz kalgiz commented Mar 23, 2018

Fixed in #6438.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.