ConvertTo-JSON an array with a single item

前端 未结 4 1009
失恋的感觉
失恋的感觉 2020-12-03 16:58

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 exp

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-03 17:17

    I faced this issue with an array that was a child in an object. The array had one object in it, and ConvertTo-Json was removing the object in the array.

    Two things to resolve this:

    I had to set the -Depth parameter on ConvertTo-Json

    $output = $body | ConvertTo-Json -Depth 10
    

    I had to create the object in the array as a hashtable and then convert that to an object

    $myArray.Add([pscustomobject]@{prop1 = ""; prop2 = "" })
    

提交回复
热议问题