Search code examples
powershellpowershell-3.0

ConvertTo-JSON an array with a single item


I'm trying to create a JSON-serialized array. When that array contains only one item I get a string, not an array of strings (in JSON).

Multiple Items (works as expected):

PS C:\> @("one", "two") | ConvertTo-JSON
[
    "one",
    "two"
]

Single Item Array (not as expected):

PS C:\> @("one") | ConvertTo-JSON
"one"

Am I missing something?


Solution

  • Try without the pipeline:

    PS C:\> ConvertTo-Json @('one', 'two')
    [
        "one",
        "two"
    ]
    PS C:\> ConvertTo-Json @('one')
    [
        "one"
    ]