Data Objects in Orchestrator

How to build and parse common data types in vRO scripting: JSON, XML, PowerShell scripts, Properties, arrays, dates and composite types, with small tested examples to get started in VCF Operations Orchestrator.

In this article

In Orchestrator scripting you build and read data all the time: JSON bodies for REST calls, XML for older APIs, PowerShell scripts for remote hosts, and structured values that move between workflow steps. This post collects the simple patterns I use for each data type, plus how to parse them back into something you can work with.

Tested on: VCF Operations Orchestrator 9.1.1 (embedded in VCF Automation). All the snippets are plain ES5, so they should look familiar on 8.x as well.

JSON

Creating JSON objects in vRO is pretty simple and straight forward. I mainly use two different methods, depending on the complexity. Method one is creating the object using string concatenation (useful for small and simple objects), and method two is the Properties object, which is, my opinion, better for more complex objects, especially if initially I don’t know how many entries the object is going to have or adding arrays.

Method one

var jsonObject =
{
"name": "Matthias",
"company": "comdivision",
"hobbies": [
"cooking","skiing"
]
}

As we can see, method one is straight forward. I added an array of strings in my example, but it could be an array of properties again. Instead of adding a string as value, input parameters of the scriptable task could be used too, just be careful with the type.

Method two

var jsonObject = new Properties();
jsonObject.name = "Matthias";
jsonObject.company = "comdivision";
jsonObject.hobbies = ["cooking","skiing"];

The second method, as we all can see, is easier and provides a better readability, especially if it comes to large objects.

Parsing JSON

Most REST APIs hand you JSON as a string, for example in response.contentAsString. JSON.parse() turns it into an object you can walk with dots, and JSON.stringify() turns it back into a string for the next request. The last two arguments of JSON.stringify() pretty-print the output, which is handy in logs.

var jsonString = '{"name":"web01","cpu":2,"tags":["prod","linux"],"disk":{"sizeGb":40}}';
// string -> object
var vm = JSON.parse(jsonString);
System.log(vm.name + " has " + vm.cpu + " vCPU, a " + vm.disk.sizeGb + " GB disk and tag " + vm.tags[0]);
// object -> string (pretty printed with 2 spaces)
vm.cpu = 4;
System.log(JSON.stringify(vm, null, 2));

XML

For XML we can use two different main methods, but I am sure, there are other possibilities as well. I am using these two, because, especially the second one, it’s easy and straight forward.

Method one

In the first method, I use simple string concatenation. If the XML object is small, this is a fast and simple approach. It even allows adding dynamic values. As an example, I am going to reuse NSX stuff, creating the XML for the NSX controller deployment, because this is a small and understandable XML structure.

var xmlString =
[
'<controllerSpec>',
' <name>' + controllerName + '</name>',
' <description>nsx-controller</description>',
' <ipPoolId>ipPool-1</ipPoolId>',
' <resourcePoolId>domain-c1</resourcePoolId>',
' <datastoreId>datastore-1</datastoreId>',
' <networkId>dvportgroup-1</networkId>',
' <password>' + passwordSecureString + '</password>',
'</controllerSpec>'
].join('\n');

This example uses simple string concatenation. Please recognize the square brackets, surrounding all the strings, with the .join method at the end. For “name” and “password” I added dynamic values which are in parameters for the scriptable task to demonstrate how to add dynamic values. The downside of this method is, the result is a string and not an XML object which makes it less flexible for later editing. Of course, it could be converted later.

Method two

I am using the same XML but this time I create an E4X object straight ahead, which allows to easily add and/or change values. This is very useful for large XML objects. Be aware, if you’re not changing the value of a specific tag, the value provided during the object creation will remain.

var controllerSpec =
<controllerSpec>
<name/>
<description>nsx-controller</description>
<ipPoolId>ipPool-1</ipPoolId>
<resourcePoolId>domain-c1</resourcePoolId>
<datastoreId>datastore-1</datastoreId>
<networkId>dvportgroup-1</networkId>
<password/>
</controllerSpec>;
controllerSpec.name = controllerName;
controllerSpec.description = "NSX Controller Cluster Management";
controllerSpec.ipPoolId = ipPoolId;
controllerSpec.resourcePoolId = resourcePoolId;
controllerSpec.datastoreId = datastoreId;
controllerSpec.networkId = portGroupId;
controllerSpec.password = passwordSecureString;

Using E4X allows us accessing the tags using a dot. This is great if we deal with larger XML data structures and especially nested structures. Be aware, the second method creates an XML object. If you would like to use it for REST calls, it needs to be converted into string upfront using the .toString() method.

Parsing XML

When an API returns XML as a string, you can parse it either way. With E4X, pass the string to new XML(). Remove the <?xml ...?> declaration first, because E4X does not accept it. Then use dots for child elements and @ for attributes.

var vmsXml = '<?xml version="1.0"?><vms><vm id="1"><name>web01</name><cpu>2</cpu></vm><vm id="2"><name>db01</name><cpu>4</cpu></vm></vms>';
var vms = new XML(vmsXml.replace(/<\?xml[^>]*\?>/, ""));
System.log("Found " + vms.vm.length() + " VMs");
for (var i = 0; i < vms.vm.length(); i++) {
var vm = vms.vm[i];
System.log("VM " + vm.@id + ": " + vm.name + " with " + vm.cpu + " vCPU");
}

If you prefer the DOM style (or you are porting code from Java), the XML plug-in has XMLManager.fromString(). It is happy with the declaration and gives you getElementsByTagName() and getAttribute().

var doc = XMLManager.fromString(vmsXml);
var nodes = doc.getElementsByTagName("vm");
for (var i = 0; i < nodes.length; i++) {
var node = nodes.item(i);
var name = node.getElementsByTagName("name").item(0).textContent;
System.log("VM " + node.getAttribute("id") + ": " + name);
}

PowerShell

Creating larger powerShell scripts might be tricky in vRO. I am currently using the already mentioned string concatenation method. It works great for all kind of scripts created for diverse command lines like bash.

For this article, I am using a, what I think, very useful example. I had to deal with powerShell scripts being identical but are executed against various hosts in different domains. I guess, we’re all aware of the double hop issue we have and all the different necessary configurations to get this up and running. What I do is creating a PSCred object on the fly and add it to the script as authentication method used with different cmdlet’s.

var powerShellScript =
[
'$password = "' + passwordSecureString + '" | ConvertTo-SecureString -asPlainText -Force',
'$username = "' + myCustomUsername + '"',
'$credential = New-Object System.Management.Automation.PSCredential($username,$password)',
'gwmi win32_service -credential $credential -computer $computer'
].join('\n');

Properties

Properties is the key-value type you will see everywhere in vRO: workflow inputs, vRA/VCFA payloads and action results. It has only a few methods (put, get, remove and the keys array), and there is no containsKey(), so check for null instead. You can also read and set values with a dot.

var props = new Properties();
props.put("vmName", "web01");
props.put("cpu", 2);
props.owner = "mayank"; // dot works too
System.log("vmName = " + props.get("vmName"));
System.log("has memory? " + (props.get("memory") != null));
// loop over all keys
var keys = props.keys;
for (var i = 0; i < keys.length; i++) {
System.log(keys[i] + " -> " + props.get(keys[i]));
}
props.remove("owner");

Properties and JSON convert in both directions. JSON.stringify() works on a Properties object directly, and the constructor accepts a plain object, so a parsed JSON string becomes a Properties object in one line.

var asJson = JSON.stringify(props); // {"vmName":"web01","cpu":2}
var fromJson = new Properties(JSON.parse(asJson)); // back to Properties
System.log(fromJson.get("cpu"));

Flattening nested objects

A nested object is easy to read with dots, but sometimes you need it flat: to compare two payloads key by key, to log every value, or to pass it to something that only takes simple key-value pairs, like custom properties or a CSV row. The small recursive function below walks the object and puts every leaf value into one Properties object. Each key is the full path joined with $, and array positions become numbers.

// flatten a nested object into one level, keys joined with "$"
function flatten(source, prefix, target) {
for (var key in source) {
var value = source[key];
var path = prefix ? prefix + "$" + key : key;
if (value !== null && typeof value === "object" && !(value instanceof Date)) {
flatten(value, path, target);
} else {
target.put(path, value);
}
}
return target;
}
var vm = JSON.parse('{"name":"web01","disk":{"sizeGb":40,"thin":true},"tags":["prod","linux"],"nics":[{"network":"app-net"}]}');
var flat = flatten(vm, "", new Properties());
var keys = flat.keys;
for (var i = 0; i < keys.length; i++) {
System.log(keys[i] + " : " + flat.get(keys[i]));
}

The log shows one line per value, with the path as the key:

name : web01
disk$sizeGb : 40
disk$thin : true
tags$0 : prod
tags$1 : linux
nics$0$network : app-net

If you want this as a ready-made action, Stefan Schnell has published one on his IT Automation Blog: Flatten Complex Objects. It takes a Properties input and returns the flattened Properties, and it also keeps empty objects and empty arrays, which the short version above skips. One thing to keep in mind with either version: a key that already contains a $ makes the flattened path unclear, so check your key names first.

Arrays

Arrays in vRO are typed when they are inputs, outputs or attributes, for example Array/string or Array/VC:VirtualMachine. In scripting they are ordinary JavaScript arrays. Remember that Orchestrator is ES5, so there is no includes(), find() or arrow functions. Use indexOf() and a plain for loop instead.

var vmNames = ["web01", "db01"];
vmNames.push("app01");
System.log("count: " + vmNames.length);
System.log("has db01? " + (vmNames.indexOf("db01") !== -1));
vmNames.sort();
System.log("sorted: " + vmNames.join(", ")); // app01, db01, web01
// filter the ES5 way
var vms = [{ name: "web01", cpu: 2 }, { name: "db01", cpu: 8 }];
var bigVms = [];
for (var i = 0; i < vms.length; i++) {
if (vms[i].cpu > 4) {
bigVms.push(vms[i].name);
}
}

Date

Dates come up in schedules, lease times and API filters. Most APIs send dates as ISO 8601 strings, and new Date() parses those directly. toISOString() goes the other way, and JSON.stringify() writes dates in the same ISO format. For date maths, work in milliseconds.

var now = new Date();
System.log("now: " + now.toISOString());
// parse an ISO string from an API
var created = new Date("2026-11-13T10:30:00Z");
if (isNaN(created.getTime())) {
throw "Invalid date received";
}
// add 7 days
var leaseEnd = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000);
System.log("lease ends: " + leaseEnd.toISOString());
System.log(JSON.stringify({ created: created })); // {"created":"2026-11-13T10:30:00.000Z"}

Composite types

A composite type groups a few typed fields into one workflow parameter, like a small structure. They exist only on workflows (inputs, outputs and attributes), not on actions. In the workflow editor you add the fields one by one. In the workflow schema and in the API the type looks like this:

CompositeType(label:string,sizeGb:number,thin:boolean):Disk
Array/CompositeType(label:string,sizeGb:number,thin:boolean):Disk

Inside a scriptable task a composite value arrives as a Properties object, so you read its fields with a dot (or get()). An array of composites is a plain array of those objects.

// inputs: disk (Disk), disks (Array of Disk)
System.log("disk: " + disk.label + " / " + disk.sizeGb + " GB / thin " + disk.thin);
for (var i = 0; i < disks.length; i++) {
System.log(disks[i].label + " = " + disks[i].sizeGb + " GB");
}

To fill a composite output or attribute, assign a Properties object, a plain object literal, or a parsed JSON string. The field names must match the composite type definition.

// outputs: newDisk (Disk), bigDisks (Array of Disk), disksJson (string)
var d = new Properties();
d.label = "logs";
d.sizeGb = 100;
d.thin = true;
newDisk = d;
bigDisks = [];
for (var i = 0; i < disks.length; i++) {
if (disks[i].sizeGb > 50) {
bigDisks.push(disks[i]);
}
}
bigDisks.push({ label: "backup", sizeGb: 500, thin: false }); // object literal
bigDisks.push(new Properties(JSON.parse('{"label":"temp","sizeGb":20,"thin":true}'))); // from JSON
// and back to JSON, for example for a REST body
disksJson = JSON.stringify(disks);

Composite types are great for grouped inputs like disks or network adapters, but they are less handy outside the workflow: actions cannot take them and some callers cannot pass them. If a value needs to travel further, a Properties object or a JSON string is usually the easier choice.

Wrapping up

That covers the data types I use most in Orchestrator scripting: JSON and Properties for most payloads, XML (E4X or DOM) for older APIs, string arrays for PowerShell scripts, plain arrays and dates, and composite types for structured workflow inputs. Start with the simplest one that does the job, and parse into objects as early as possible so the rest of the script stays readable.


Discover more from Cloud Blogger

Subscribe to get the latest posts sent to your email.

Enjoyed this post?

Was this helpful?

Leave a Reply