Generating JSON Data Within UCS Director Custom Workflow Tasks

Version 3

    Should you ever require to create JSON formatted variables within a custom workflow task (When making RESTful requests to external systems for example), then the following steps will allow you to achieve this.

     

    By the end of this exercise, we will use UCS Director to create a variable that contains the following reasonably complex JSON string:

     

    {

        "pool":"test-http-pool",

        "description":"My test virtual server",

        "name":"test-http-virtual",

        "mask":"255.255.255.255",

        "profiles":[

            {

                "name":"http",

                "kind":"ltm:virtual:profile"

            },

            {

                "name":"tcp",

                "kind":"ltm:virtual:profile"

            }

        ],

        "ipProtocol":"tcp",

        "sourceAddressTranslation":{"type":"automap"},

        "kind":"tm:ltm:virtual:virtualstate",

        "destination":"1.1.1.3:80"

    }

     

     

    If needs be, a short JSON refresher can be found here:

     

    https://communities.cisco.com/docs/DOC-57426

     

    So, let’s get started.

     

    Firstly, we are going to store our keys and values in a Java HashMap. Therefore, we need to start our CloupiaScript with the following:

     

    importPackage(java.util);

     

    Whilst we are here, let’s also import the following package which contains the class to convert a Java HashMap to a JSON string:

     

    importPackage(com.cloupia.lib.util);

     

    We can now go ahead and create a HashMap to store our key/value pairs:

     

    var map = new HashMap();

     

    Let’s start slow and build a variable that contains a single JSON element.

     

    map.put("name","test-http-virtual");

     

    Notice that the delimeter for the HashMap is a comma (Not a colon as is normal for JSON). For this very simple example, let’s create a new variable that contains the JSON formatted data for this HashMap:

     

    var data = JSON.javaToJsonString(map, map.getClass());

     

    This operation can be verified within UCS Director's CloupiaScript interpreter, which can be found under Policies -> Orchestration -> Custom Workflow Tasks.

    Screen Shot 2015-05-10 at 01.29.29.png

     

    The other simple key/value pairs can now be added to our HashMap like so:

     

    map.put("kind","tm:ltm:virtual:virtualstate");

    map.put("destination","1.1.1.3:80");

    map.put("mask","255.255.255.255" );

    map.put("ipProtocol","tcp");

    map.put("pool","test-http-pool");

    map.put("description","My test virtual server");

     

    var data = JSON.javaToJsonString(map, map.getClass());

    Screen Shot 2015-05-10 at 01.34.32.png

     

    Variable ‘data’ now looks like so:

     

    {

        "pool":"test-http-pool",

        "description":"My test virtual server",

        "name":"test-http-virtual",

        "mask":"255.255.255.255",

        "ipProtocol":"tcp",

        "kind":"tm:ltm:virtual:virtualstate",

        "destination":"1.1.1.3:80"

    }

     

    In all of the previous examples, the ‘value’ has always be a single entry (In this case, a string). However, it is possible for the value to be another JSON element. Like so:

     

    "sourceAddressTranslation":{"type":"automap"}

     

    In this case, we need to create a second HashMap that will contain the key/value pair of “type” and “automap”. This new HashMap is then supplied as a value to the original HashMap. Like so:

     

    var map2 = new HashMap();

    map2.put("type","automap");

    map.put("sourceAddressTranslation",map2);

    Screen Shot 2015-05-10 at 01.36.07.png

     

    The variable ‘data’ now looks like so:

     

    {

        "pool":"test-http-pool",

        "description":"My test virtual server",

        "name":"test-http-virtual",

        "mask":"255.255.255.255",

        "ipProtocol":"tcp",

        "sourceAddressTranslation":{"type":"automap"},

        "kind":"tm:ltm:virtual:virtualstate",

        "destination":"1.1.1.3:80"

    }

     

    Finally, we need to look at the most complex JSON element in our example:

     

    "profiles":[

            {

                "name":"http",

                "kind":"ltm:virtual:profile"

            },

            {

                "name":"tcp",

                "kind":"ltm:virtual:profile"

            }

        ],

     

    In this case, the value of key “profiles” contains an array of JSON elements. To accomplish this, we first add the key “profiles” to our HashMap, but declare its value as an empty array:

     

    map.put("profiles",new ArrayList());

     

    To convert this to a JSON string now would look like so:

     

    {

        profiles":[]

    }

     

    We now want to add our JSON elements to the array created for the “profiles” key. We accomplish this like so:

     

    var profile = new HashMap();

    profile.put("kind","ltm:virtual:profile");

    profile.put("name","http");

    map.get("profiles").add(profile);

     

    var profile = new HashMap();

    profile.put("kind","ltm:virtual:profile");

    profile.put("name","tcp");

    map.get("profiles").add(profile);

     

    This gives us the final version of our desired JSON content:

     

    Screen Shot 2015-05-10 at 01.38.14.png

     

    Finally, our variable ‘data’ looks like so:

     

    {

        "pool":"test-http-pool",

        "description":"My test virtual server",

        "name":"test-http-virtual",

        "mask":"255.255.255.255",

        "profiles":[

             {

                 "name":"http",

                 "kind":"ltm:virtual:profile"

             },

             {

                 "name":"tcp",

                 "kind":"ltm:virtual:profile"

             }

        ],

        "ipProtocol":"tcp",

        "sourceAddressTranslation":{"type":"automap"},

        "kind":"tm:ltm:virtual:virtualstate",

        "destination":"1.1.1.3:80"

    }