Parsing JSON Objects within UCS Director Custom Workflow Tasks

Version 5

    This document partners the document ‘Generating JSON Data Within UCS Director Custom Workflow Tasks’ which can be found here:



    However, rather than creating JSON objects from Java HashMaps, this document describes how to parse JSON strings within UCS Director.


    For this exercise, we will use the same JSON string that we created in the previous document:




        "description":"My test virtual server",



















    Also, this document will demonstrate how to verify the existence of the element being requested. It will also demonstrate how to test whether the element is a JSON object or a JSON array. In the event of an array, it will also demonstrate how to retrieve the array length.


    So, let’s get started.


    Once again, we need to  access the JSON parsing functionality within UCS Director,  and therefore need to start our CloupiaScript with the following:




    Next, we’ll configure a variable with the JSON string example shown at the start of this document:


    var jsonData = '{"pool":"test-http-pool","description":"My test virtual server","name":"test-http-virtual","mask":"","profiles":[{"name":"http","kind":"ltm:virtual:profile"},{"name":"tcp","kind":"ltm:virtual:profile"}],"ipProtocol":"tcp","sourceAddressTranslation":{"type":"automap"},"kind":"tm:ltm:virtual:virtualstate","destination":""}';


    The next step is to consume this string as a JSON object:


    var data = JSON.getJsonElement(jsonData,null);


    In this case, jsonData is the JSON string that we are looking to parse, and null simply means to parse the entire string. Had we just wanted to retrieve the ‘profiles’ element, then the request would look like so:


    var data = JSON.getJsonElement(jsonData, "profiles");


    Screen Shot 2015-05-12 at 16.40.07.png


    When incorporating conditional branching and validation within custom workflow tasks, it is desirable to ascertain ahead of time information about the JSON object or array that is being retrieved. Below are various validation requests that can be performed upon the JSON string.


    Once we have set a variable as a JSON element, the following methods can be used:


    isJsonObject() – Returns boolean true if element is a JSON object.

    isJsonArray() – Returns boolean true if element is a JSON array.

    isString() – Returns boolean true if element is a String.

    isNumber() – Returns boolean true if element is a number.

    isBoolean() – Returns boolean true if element is a boolean.

    isJsonNull() - Returns boolean true if element is Null.




    The following commands, run within UCS Director’s CloupiaScript Interpreter demonstrate the above methods:


    Screen Shot 2015-05-12 at 16.41.23.png


    Existence of a specific key can be checked programmatically with the has() method.





    Screen Shot 2015-05-12 at 16.42.35.png


    Also, when the element is a JSON array, it is possible to ascertain the length of the array in order to programmatically iterate through it. Once the element has been determined to be a JSON array, the size() method can be used to find the length of the array.


    var data = JSON.getJsonElement(jsonData, "profiles");








    Screen Shot 2015-05-12 at 16.44.42.png


    Once the array size is ascertained, the individual array elements can be retrieved with the get() method.


    data.get(0); // 0 is index zero of the array.



    data.get(1); // 1 is index one of the array.




    You now have all the tools required to retrieve any JSON element.


    var data = JSON.getJsonElement(jsonData,null);





    Screen Shot 2015-05-12 at 16.47.00.png


    Finally, the retrieved elements are still JSON objects. The getAsString() method can be used in order to retrieve the JSON element as a String:


    var data = JSON.getJsonElement(jsonData,null);


    var str = data.get("pool");


    str.length(); TypeError: Cannot find function length. (<Unknown source>#1) in <Unknown source> at line number 1


    str = data.get("pool").getAsString();