vRO XML & JSON Gotcha! – Part 2

In the first post, I showed you the basics syntax to work with XML and JSON within vRO.

We learn how to create, update and delete values in XML and JSON object, but in real life we some more complex objects such as arrays or we need to convert string to XML or JSON. In this post, I’ll show you some more advanced tricks.

First, let’s see how to work with arrays within XML and JSON objects.

Update XML element with array of XML objects:

So let’s say we want to add multiple “member” objects to the “members” element, each member has name and version elements, so the pool object will be like this:

  1. <pool>
  2.   <name>myPool</name>
  3.   <version/>
  4.   <members>
  5.     <member>
  6.       <name>member01</name>
  7.       <version>1.1</version>
  8.     </member>
  9.     <member>
  10.       <name>member02</name>
  11.       <version>1.1</version>
  12.     </member>
  13.   </members>
  14. </pool>

 

  1. var poolXml = new XML(‘<pool><name/><version/><members/></pool>’);
  2. poolXml.name=“myPool”;
  3. var memberXml = new XML(‘<member><name/></member>’);
  4. memberXml.name=“member01”;
  5. memberXml.version=“1.1”;
  6. poolXml.members.appendChild(memberXml.toString());
  7. memberXml.name=“member02”;
  8. memberXml.version=“1.1”;
  9. poolXml.members.appendChild(memberXml.toString());

 

You can do it within a loop of course.

Iterate between members:

  1. for each (member in poolXml.members.member){
  2.     System.log(member.name);
  3. }

 

Update JSON attribute with array of JSON objects:

  1.     var poolJSON = {
  2.            “name”: “”,
  3.             “version”: “”,
  4.             “members”: []
  5.         };
  6.     poolJSON.name = “myPool”;
  7.     var memberJSON = {
  8.            “name”: “myMember01”,
  9.            “version”: “1.1”
  10.         };
  11.     poolJSON.members.push(memberJSON);
  12.     memberJSON.name = “myMember02”;
  13.     memberJSON.version = “1.1”;
  14.     poolJSON.members.push(memberJSON);

 

Will give us:

  1. {
  2.   “name”: “myPool”,
  3.   “version”: “”,
  4.   “members”: [
  5.     {
  6.       “name”: “myMember01”,
  7.       “version”: “1.1”
  8.     },
  9.     {
  10.       “name”: “myMember02”,
  11.       “version”: “1.1”
  12.     }
  13.   ]
  14. }

 

Iterate between members:

  1. for each (member in poolJSON.members) {
  2.     System.log(member.name);
  3. }

 

When running REST calls within vRO using the REST plugin, the responses will be strings (JSON/XML depends on the header that you set), so if we need to extract some information or values from this strings without regex or string manipulation, we will need to convert this string to JSON or XML.

String to XML:

Let’s say that we got this string that we got from a REST call response:

  1. var xmlString = “<pool><name>myPool</name><version/><members><member><name>member01</name><version>1.1</version></member><member><name>member02</name><version>1.1</version></member></members></pool>”;

 

If just want the version of the members, substring? NO WAY!

So, what we need to do is:

  1. var string2xml = new XML(xmlString);

Now, we can iterate between the members version as we learn in the previews post.

XML to String:

OK, now the opposite scenario, we created the XML object for the REST call body, but we need to convert it to string in order to pass it in within the POST call.

  1. var xml2String = string2xml.toString();

 

String to JSON:

Now we have this JSON string:

  1. var jsonString = “{“name“:”myPool“,”version“:””,”members“:[{“name“:”myMember01“,”version“:”1.1“},{“name“:”myMember02“,”version“:”1.1“}]}”; 

 

In order to convert this string to JS object:

  1. var string2json = JSON.parse(jsonString);

 

JSON to String:

OK, now again the opposite scenario:

  1. var json2string = JSON.stringify(string2json);

 

Good, now… if I really like to work with JSON or I need to take information from one system to another and I have an XML object and I want to convert it to JSON, how can I do it?

We can use an out of the box action that comes with the REST plugin called “xml2json”, and we use it like this:

  1. var xml2json = System.getModule(“com.vmware.library.http-rest”).xml2json(xmlObject.toString());

 

Now you can work with the xml2json object as we learn in the previews post.

 

So this is it… I think that this is the fundamentals syntax that you will need to know in order to work with XML and JSON within vRO.

 

Hope you enjoyed it! If you have feedback, please feel free to comment below.

 

Leave a Reply