$parent = @"
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubTemplateProps": {
SubSubSubTemplateProps1 : "SubSubSubTemplateProps1",
SubSubSubTemplateProps2 : {
SubSubSubSubTemplateProps1 : [
{
"Key": "Name",
"Value": "Temp"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
"@ | ConvertFrom-Json
$templateobj = $parent.SubProperties.SubSubTemplateProps
$parent.SubProperties.psobject.Properties.Remove("SubSubTemplateProps")
for($j = 0; $j -lt 3; $j++)
{
$i = "{0:D2}" -f ($j+1)
$letters = @("a","b","c")
foreach($letter in $letters)
{
$newobject = $templateobj
($newobject.SubSubSubTemplateProps2.SubSubSubSubTemplateProps1 | ? {$_.key -eq "Name"}).Value = "NewValue$letter$i"
$parent.SubProperties | Add-Member -MemberType NoteProperty -Name "SubSubProp$letter$i" -Value $newobject
}
}
$parent |convertto-json -Depth 100
Im expecting output like this:
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubPropa01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValuea01"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb01"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropa02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValuea02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
But I get output like this where all of the noteproperties of the parent are updated instead of just the noteproperty I added. WHAT?!
{
"Property1": "Property1Value",
"Description": "Generic Description",
"SubProperties": {
"SubSubPropa01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb01": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropa02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
},
"SubSubPropb02": {
"SubSubSubTemplateProps1": "SubSubSubTemplateProps1",
"SubSubSubTemplateProps2": {
"SubSubSubSubTemplateProps1": [
{
"Key": "Name",
"Value": "NewValueb02"
},
{
"Key": "SupSubSubProp2",
"Value": "Supprop2value"
}
]
}
}
}
}
Can someone suggest how to copy a node and replace a sub value then add it back to the parent without it overwriting all other child nodes? Not sure why this is happening.