vRO XML & JSON Gotcha! – Part 1

When integrating vRealize Orchestrator with external systems using REST API, knowing how to work with XML and JSON is a must!

During my work on the NSX Firewall as a Service Package and on the F5 LTM Dynamic Types plugin, I noticed that we have a lot of “gotchas” that are not very well documented.

Basically, this isn’t vRO special syntax, it’s JavaScript syntax that I’ll show you how to use in this post.

My code samples for both XML and JSON will be based on a custom object that we will call it “Pool”. This “Pool” object as attributes such as name, version, and members of this pool.

Create object/Initialization:

XML object:

  1. <pool>
  2.     <version>1.1</version>
  3.     <name>myPool</name>
  4.     <members></members>
  5. </pool>

 

Create XML object option 1:

  1. var poolXml = new XML(‘<pool><version/><name/><members/></pool>’);

 

Create XML object option 2:

  1. var poolXml = ‘<pool><version/><name/><members/></pool>’;

 

JSON object:

  1. {
  2.     “name”: “myPool”,
  3.     “version”: “1.1”,
  4.     “members”: []
  5. }

 

Create JSON object:

  1. poolJSON = {
  2.    “name”: “myPool”,
  3.     “version”: “1.1”,
  4.     “members”: []
  5. };

 

As you may have noticed, in case of using JSON object with array of attributes, you need to declare it in the initialization step, while in XML you don’t.

 

Update XML Attributes/Elements:

In XML we have Attributes and Elements, what’s the different?

In this example the “name” is an element attribute:

  1. <pool name=”myPool>
  2.     <version>1.1</version>
  3.     <members></members>
  4. </pool>

 

In this example the “name” is a child element:

  1. <pool>
  2.     <version>1.1</version>
  3.     <name>myPool</name>
  4.     <members></members>
  5. </pool>

 

Another important note is that in XML we have a “root” element while in JSON we don’t. In our example, the root element is “pool”.

Update element attribute:

  1. var poolXml = new XML(‘<pool><version/><members/></pool>’);
  2. poolXml.@name=“myPool”;

 

Will give us:

  1. <pool name=“myPool”>
  2.   <version/>
  3.   <members/>
  4. </pool>

 

Update element value:

  1. var poolXml = new XML(‘<pool><name/><version/><members/></pool>’);
  2. poolXml.name=“myPool”;

 

Will give us:

  1. <pool>
  2.   <name>myPool</name>
  3.   <version/>
  4.   <members/>
  5. </pool>

 

If you have many elements that you need to initialize with a value, you don’t need to declare them, so it will be like this:

Update element value:

  1. var poolXml = new XML(‘<pool><members/></pool>’);
  2. poolXml.name=“myPool”;
  3. poolXml.version=“1.1”;

 

Will give us:

  1. <pool>
  2.   <name>myPool</name>
  3.   <version>1.1</version>
  4.   <members/>
  5. </pool>

 

You don’t need to type the root element when updating elements or attributes values!

 

Update JSON Attributes:

In JSON it’s easier:

  1. var poolJSON = {
  2.        “name”: “”,
  3.         “version”: “”,
  4.         “members”: []
  5.     };
  6. poolJSON.name = “myPool”;

 

Will give us:

  1. {
  2.   “name”“myPool”,
  3.   “version”“”,
  4.   “members”: [
  5.     
  6.   ]
  7. }

 

Delete XML Element & Attribute:

Delete attribute:

  1. var poolXml = new XML(‘<pool><name/><version/><members/></pool>’);
  2. poolXml.@name=“myPool”;
  3. delete poolXml.@name;

Delete element:

  1. var poolXml = new XML(‘<pool><name/><version/><members/></pool>’);
  2. poolXml.name=“myPool”;
  3. delete poolXml.name;

 

Delete JSON Attributes:

  1. var poolJSON = {
  2.        “name”: “”,
  3.         “version”: “”,
  4.         “members”: []
  5.     };
  6. poolJSON.name = “myPool”;
  7. delete poolJSON.name;

 

 

So this is for part 1! In the next post we will go through the following:

  1. Working with array within XML and JSON
  2. How to convert strings to XML or JSON
  3. How to convert XML to JSON

 

Thanks for reading!

 

One thought on “vRO XML & JSON Gotcha! – Part 1

  1. Pingback: vRO XML & JSON Gotcha! – Part 2 | VIRTUAL 'EM ALL!

Leave a Reply