Creating json objects in Orchestrator

Recently I saw a pair of articles by @RoeyAzroel on vRO XML & JSON Gotcha! - Part 1 and Part 2. This reminded me that I had some sample code that I refer to when working with json as well so figured I would share here.

Be sure you know about the JSON object and its .parse and .stringify methods since they do not appear anywhere in the Orchestrator’s API explorer or documentation.

  • .parse : This method takes a json string as input and returns a json object. You may then access the properties of that object!
  • .stringify : This method takes a json object as input and returns the json string.

Since vRealize Orchestrator (vRO, formerly vCenter Orchestrator/vCO) uses javascript as its scripting language in workflows and actions, dealing with json is actually quite easy. But in order for it to be easy, you need some basic examples of common approaches to your needs.

Assuming you get some object with variable properties that you may not know ahead of time, for example this Properties object:

1
2
3
4
5
var result = new Properties();
result.put("Make", "Toyota");
result.put("Model", "Camry");
result.put("Year", "2003");
result.put("Condition", "Good");

The above is a simple properties object, but if we received an object that had variable properties in it, how would we go about creating or converting to json? The following methods/syntax may be used when dealing with any number of sources for the data. I chose the properties object above for its simplicity and ease of providing an example.

METHOD 1 : Dynamic Properties, Object build -> Stringify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
System.debug("==== METHOD 1 : Dynamic Properties, Object build -> Stringify ====");
var jsonObj = new  Object();
for each (key in result.keys){
  jsonObj[key]=result.get(key); // Note the use of square brackets here containing a variable name!
}
// Now, let's see what we ended up with:

System.debug(JSON.stringify(jsonObj));
/*
    As you can see, the following json string is output:
    {"Make":"Toyota","Model":"Camry","Year":"2003","Condition":"Good"}
*/

METHOD 2 : Static Properties, String build -> Object -> Stringify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
System.debug("==== METHOD 2 : Static Properties, String build -> Object -> Stringify ====");
// We can also build out the json as a string - NOT PREFERRED or recommended!
var jsonString = '{';
jsonString += '"Make":"'+result.get("Make")+'",';
jsonString += '"Model":"'+result.get("Model")+'",';
jsonString += '"Year":"'+result.get("Year")+'",';
jsonString += '"Condition":"'+result.get("Condition")+'",';
jsonString += '}';
var jsonObj = JSON.parse(jsonString); // make sure we get a valid object here. 
System.debug(JSON.stringify(jsonObj)); // Now display the resulting json
/*
    As you can see, same output as before:
    {"Make":"Toyota","Model":"Camry","Year":"2003","Condition":"Good"}
*/

METHOD 3 : Static properties, Object build -> Stringify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
System.debug("==== METHOD 3 : Static properties, Object build -> Stringify ====");
var jsonObj = new  Object();
jsonObj["Make"] = result.get("Make");
jsonObj["Model"] = result.get("Model");
jsonObj["Year"] = result.get("Year");
jsonObj["Condition"] = result.get("Condition");

// Now, let's see what we ended up with:
System.debug(JSON.stringify(jsonObj));
/*
    As you can see, the following json string is output:
    {"Make":"Toyota","Model":"Camry","Year":"2003","Condition":"Good"}
*/

METHOD 4 : Static properties, Object build with dot operator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
System.debug("==== METHOD 4 : Static properties, Object build with dot operator ====");
var jsonObj = new  Object();
jsonObj.Make = result.get("Make");
jsonObj.Model = result.get("Model");
jsonObj.Year = result.get("Year");
jsonObj.Condition = result.get("Condition");

// Now, let's see what we ended up with:
System.debug(JSON.stringify(jsonObj));
/*
    As you can see, the following json string is output:
    {"Make":"Toyota","Model":"Camry","Year":"2003","Condition":"Good"}
*/

That’s it for now! If you have additional content that can be added to this article, leave a comment! Also, be sure to review those two articles by Roey, there’s some good info there!