PS: Adding nodes to JSON object

02 Aug

Whilst working on vRA REST API to provision new virtual machines, one of task was to get the template for that catalog item and fill in required values.

One of the values common to most templates is the datacenter_location value. By default it will be null. That is not a big deal if you don’t care which datacenter your VM is being provisioned to, but what if you want to specify a location?

"_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location":  null,

In this case the values should be as such

 "_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location": {
               "classId": "Infrastructure.Compute.DataCenterLocations",
               "id": "DC1"

This is how you can add the values into the JSON object (I am assuming that you already gotten the bearer token in the $vratoken variable)

$header = @{ "Accept"="application/json"; "Content-Type"="application/json"; "Authorization"="Bearer "+ $ }

$query = "{catalogitemID}/requests/template"

$form = Invoke-RestMethod -Method Get -Uri $query -Headers $header -UseDefaultCredentials:$false -ErrorAction Stop

#the datacenter location field is located here ->


$dc =[pscustomobject]@{

"classId" = "Infrastructure.Compute.DataCenterLocations" ;

"id" = "DC1" }

${catalogname}.data.datacenter_location = $dc

This is just a simple add, I have not explored more complicated structures, but I assume that you will just keep nesting pscustomobjects…lolz

Leave a comment

Posted by on August 2, 2018 in powershell, vmware


Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: