Home > Software design >  (PowerShell) Why is the for loop overwriting the JSON objects in the array? [duplicate]
(PowerShell) Why is the for loop overwriting the JSON objects in the array? [duplicate]

Time:09-29

This is the PowerShell script I am working on:

$ArrayOfArguments = @("Param1", "Param2", "Param3")
$ArrayOfArgumentDescriptions = @("Description1", "Description2", "Description3")

$jsonData = @"
{
  "foo": {
    "des": "foo-des",
    "par": [],
    "sig": "foo-sig"
  }
}
"@

$jsonParamData = @"
{
  "des": "param-des",
  "name": "param-name"
}
"@

$jsonObj = $jsonData | ConvertFrom-Json
$jsonParamObj = $jsonParamData | ConvertFrom-Json

# add param
for($i=0; $i -lt $ArrayOfArguments.count; $i  ){
    $jsonObj.foo.par  = $jsonParamObj
    $jsonObj.foo.par[$i].name = $ArrayOfArguments[$i]
    $jsonObj.foo.par[$i].des = $ArrayOfArgumentDescriptions[$i]
}

$jsonResult = $jsonObj | ConvertTo-Json -Depth 5
Write-Host $jsonResult

The output of this script is shown below:

{
    "foo":  {
                "des":  "foo-des",
                "par":  [
                            {
                                "des":  "Description3",
                                "name":  "Param3"
                            },
                            {
                                "des":  "Description3",
                                "name":  "Param3"
                            },
                            {
                                "des":  "Description3",
                                "name":  "Param3"
                            }
                        ],
                "sig":  "foo-sig"
            }
}

According to the content of my script, I would expect the output to look like this:

{
    "foo":  {
                "des":  "foo-des",
                "par":  [
                            {
                                "des":  "Description1",
                                "name":  "Param1"
                            },
                            {
                                "des":  "Description2",
                                "name":  "Param2"
                            },
                            {
                                "des":  "Description3",
                                "name":  "Param3"
                            }
                        ],
                "sig":  "foo-sig"
            }
}

It seems that the for loop is overwriting the JSON objects in the array with every iteration. Why is this happening, and how can I solve this?

CodePudding user response:

This happens because you keep adding and updating the same object reference ($jsonParamObj) to the containing array.

Use an expression that creates a new object everytime you expand the array and it won't occur:

for($i=0; $i -lt $ArrayOfArguments.count; $i  ){
    # the `[pscustomobject]@{ ... }` syntax will create a brand new object every time!
    $jsonObj.foo.par  = [pscustomobject]@{
        name = $ArrayOfArguments[$i]
         des = $ArrayOfArgumentDescriptions[$i]
    }
}
  • Related