I'm taking a list of users, and then using the following code to loop through them and build a chunk needed for a json request in a specific format.
$teamMembers = @();
foreach ($item in $userIds) {
$team = @{
id = $item
type = 'User'
roleTypes = @(
'TeamContributor'
)
}
$teamMembers += $team
}
After this hash list is created, I then need to append it to a string as follows to create a JSON string.
$body=@{name=$name;teamMembers=$teamMembers} | ConvertTo-Json -Compress
The issue i'm having is that when I run the ConvertTo-Json
, it's ignoring my roleTypes
array and treating it as a string.
If I run the ConvertTo-Json
command on just the $teamMembers
variable it looks file, but when I combine it to build the body variable that's where it goes wrong.
Here is the example of the desired format of this output:
{
"name": "Name Here",
"teamMembers": [
{
"roleTypes": [ "TeamContributor" ],
"id": "ID Here",
"type": "User"
},
{
"roleTypes": [ "TeamContributor" ],
"id": "ID Here",
"type": "User"
},
]
}
Here is what i'm getting instead:
{
"name": "Name Here",
"teamMembers": [
{
"roleTypes": "TeamContributor " ,
"id": "ID Here",
"type": "User"
},
{
"roleTypes": "TeamContributor " ,
"id": "ID Here",
"type": "User"
},
]
}
Any suggestions on the best way to accomplish this? Thank you in advance for any assistance!