0
$json = @"{"mem":[[1377691200,10],[1377770400,0]],"proc":[[1377691200,80],[1377770400,0]]}"@
$jobj = ConvertFrom-Json -InputObject $json
$datetime = 1377780400
$proc = 85
$add = @($datetime, $proc)
$jobj.proc += $($add)
cls
ConvertTo-Json $jobj  

The snippet above should add a array value to the json string but is seems to add to seperate values.

In the result the added values dont have [] and if i goto $jobj.proc[3] and [4] the values are split out of there original array. What can i do to fix this?

I found question PowerShell how to add something on parsed JSON?

But i dont have names for my entries so that wont work :(

Any clues

Community
  • 1
  • 1
Sjoerd
  • 167
  • 1
  • 9

1 Answers1

1

This seems to work:

$json = '{"mem":[[1377691200,10],[1377770400,0]],"proc":[[1377691200,80],[1377770400,0]]}'
$jobj = ConvertFrom-Json -InputObject $json
$datetime = 1377780400
$proc = 85
$add = @($datetime, $proc)
$jobj.proc += ,$add
cls
ConvertTo-Json $jobj  

Adding the leading comma in the += operation prevents "unrolling" the $add array.

mjolinor
  • 66,130
  • 7
  • 114
  • 135