Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell How to Add to JSON Array

How do you add to a JSON array in PowerShell? I'm trying with the following code, but it's complaining with a "Collection was of a fixed size" exception:

$json = @"
[
  {
    "name": "First"
  },
  {
    "name": "Second"
  }
]
"@

$toAdd =@"
{
  "name": "Third"
}
"@

$jobj = ConvertFrom-Json -InputObject $json    
$jobj.Add((ConvertFrom-Json -InputObject $toAdd))
like image 607
Andy Avatar asked Dec 05 '22 14:12

Andy


1 Answers

Just use += instead of Add():

$jobj += (ConvertFrom-Json -InputObject $toAdd)
like image 87
Martin Brandl Avatar answered Dec 12 '22 05:12

Martin Brandl