I have the following ps/json:
$a = '[{"Type":"1","Name":"A"},{"Type":"2","Name":"B"}]'
$ps = $a | ConvertFrom-Json
Type Name
---- ----
1 A
2 B
#Now, I want to add this array into another array, so the resulting json should look like this:
[[{"Type":"1","Name":"A"},{"Type":"2","Name":"B"}]]
Remove-TypeData System.Array #needed for workaround for powershell bug: https://stackoverflow.com/questions/20848507/why-does-powershell-give-different-result-in-one-liner-than-two-liner-when-conve/38212718#38212718
$w = @()
$w += , $ps
$w |ConvertTo-Json
[
{
"Type": "1",
"Name": "A"
},
{
"Type": "2",
"Name": "B"
}
]
#It's apparent that the $ps object was NOT added to an empty array as desired, however when multiple instances are added ($w += , $ps twice for example) it works as expected:
[
[
{
"Type": "1",
"Name": "A"
},
{
"Type": "2",
"Name": "B"
}
],
[
{
"Type": "1",
"Name": "A"
},
{
"Type": "2",
"Name": "B"
}
]
]
So it seems the array is there however the outer brackets are not being returned (as they should be)
I've read through other questions and could not find a resolution for this, if anyone is able to share?