How to create a valid empty JSON array using PowerShell?

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 [].

+4
source share
1 answer

Works without pipelining

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

]
+7
source

Source: https://habr.com/ru/post/1540033/


All Articles