I was working on something today and during testing I noticed a very peculiar issue
$arry = @()
$Msg = @{Body="This is a Sample Message";}
$Msg.BrokerProperties=@{}
$Msg.BrokerProperties.Label= "Msg1"
$arry += $Msg
$arry | ConvertTo-Json # 1st Result
$Msg.BrokerProperties=@{}
$Msg.BrokerProperties.Label= "Msg2"
$arry += $Msg
$arry | ConvertTo-Json
The 1st result of $arry | ConvertTo-Json
is as below
{ "Body": "This is a Sample Message", "BrokerProperties": { "Label": "Msg1" } }
The 2nd result of $arry | ConvertTo-Json
is as below
[ { "Body": "This is a Sample Message", "BrokerProperties": { "Label": "Msg2" } }, { "Body": "This is a Sample Message", "BrokerProperties": { "Label": "Msg2" } } ]
What I thought would happen is when I set $Msg.BrokerProperties.Label= "Msg2"
for 2nd time , then it would only effect the 2nd hashtable in array. but very interestingly that property is getting injected even on to 1st hashtable.
Can someone please explain this behaviour?
I was actually doing this in a loop for prepare a JSON payload for sending on to API call,so I am lookign for a way to update labels for each inner within the json object