Converting arrays to a JSON string in PowerShell couldn't be simpler:
@(1,2,3) | ConvertTo-Json
It produces:
[
1,
2,
3
]
However, if the array is empty, the result is an empty string:
@() | ConvertTo-Json
Gets an empty string instead [].
source
share