How to create a valid empty JSON array with PowerShell?

arrays, json, powershell

Solution

It works without pipelining

PS C:\> ConvertTo-Json @()
[

]

Problem

Converting arrays to JSON string in PowerShell couldn't be more simple: ``` @(1,2,3) | ConvertTo-Json ``` Produces: ``` [ 1, 2, 3 ] ``` However if the array is empty the result is an empty string: ``` @() | ConvertTo-Json ``` Results an empty string instead of `[]`.

Original source