ConvertTo-JSON an array with a single item
I hit this problem as well but it was because my structure was too deep and ConvertTo-Json flattens everything below a certain depth to a string.
For example:
PS C:\> $MyObject = @{ "a" = @{ "b" = @{ "c" = @("d") } } }
PS C:\> ConvertTo-Json $MyObject
{
"a": {
"b": {
"c": "d"
}
}
}
To fix this, you can pass a larger value to -Depth
PS C:\> ConvertTo-Json $MyObject -Depth 100
{
"a": {
"b": {
"c": [
"d"
]
}
}
}
Try without the pipeline:
PS C:\> ConvertTo-Json @('one', 'two')
[
"one",
"two"
]
PS C:\> ConvertTo-Json @('one')
[
"one"
]