Top

Questions?

Questions?

Ask

Matthias

Thank you! Your message has been sent!
Oops! Something went wrong while submitting the form.

Understanding JSON objects

by

Matthias Eisner

By
Matthias Eisner
and

First to say, JSON (JavaScript Object Notation) is an object, based on JavaScript, the programming language in vRO. This leads to the fact, that these objects can easily be created and handled in workflows. Furthermore, I often use them to pass a lot of information from one element to another. Instead of binding many input and output parameters to certain elements, you only need to create a single binding and have the option to pass many parameters at once.


Let’s start with JSON. An object consists of key – value pairs. That’s the first step. The key is always used to access the value. You have two main choices to create the JSON object. Implicitly or explicitly.

Explicit JSON object declaration:

A simple example (including implicit object declaration):

 var jsonObject = new Properties();

If you would like to access the information simply use:

var firstJsonObject =
{
      "company": "comdivision"
}

The prints the value into the log output of vRO.

System.log(firstJsonObject.company);

JSON objects are highly flexible. You can always add additional information to the object, once created.


firstJsonObject.country = "Germany";

That adds an additional key – value pair to the object.

Please note: if you use an existing key, you will overwrite the value of the given key.

This object could be built at once too, and that leads us to the next important character in a JSON object during the creation, the comma:

Again, our simple example:


var firstJsonObject =
{
      "company": "comdivision",
      "country": "Germany"
}

Please note the comma at the end of the first key – value pair. The comma indicates the next key – value pair.

These are the basics. The cool thing about a JSON object is, that the value of a key can not only be a string, like in the example I’ve used. It could also be a number, a Boolean value, and, talking about vRO, also object types provided by a plugin, e.g.: VC:VirtualMachine. Even cooler, the value could be an array of something or event another JSON object. Yes, I am talking about nesting JSON objects. This is really helpful. Of course, the object will be more complex, but again, it is still a single object.

Alright, let’s create something more complex. In the next example I add a JSON object as value for a key. Again, this can be done implicitly and explicitly. First, I’ll use the implicit method:'

var firstJsonObject =
{
      "company": "comdivision",
      "address": {
            "country": "Germany",
            "zipcode": "12345",
            "street": "my street",
            "number": "34"
      }
}

Now I’ll provide some details what I did. Inside the implicit object creation, you can use {} (curly brackets) to indicate that the value of a key is another JSON object. Inside, again the comma is used, to tell that there are a couple of different key – value pairs. Note, the last item has NO comma at the end, showing that this is the end of the object.

If you need to add a JSON object later to an existing object, I suggest using the following method:

myExistingJsonObject.address = new Properties();
myExistingJsonObject.address.country = "Germany";
myExistingJsonObject.address.zipcode = "12345";

As you can see, we add an additional key, again named address, and tell vRO, that inside this key we’re going to have another JSON object. Afterwards we just add keys and provide values for them, as we already did.

So far so good. I suggest to get back to our first example and add more stuff.

var firstJsonObject =
{
      "company": "comdivision",
      "address": {
            "country": "Germany",
            "zipcode": "12345",
            "street": "my street",
            "number": "34"
      },
      "employees": [
            {
                  "firstName": "firstName01",
                  "lastName": "lastName01"
            },
            {
                  "firstName": "firstName02",
                  "lastName": "lastName02"
            }
      ]
}

OK, now it looks weirder than before, but I did that on purpose. Let’s take a closer what I did: after the address JSON object, I added a comma, telling, here is the next key – value pair named “employees”, but instead of just providing a value, I started with a [ (square bracket) indicating an array. Inside the array, I used JSON objects again (I already explained how this is done) and finally closing the array with a ] (square bracket again).

The cool thing about the array inside the JSON object is, it can be accessed like any other array. Let’s do a short example to write all last names into the log output of vRO, using the JSON object we just created.

This results in the following log output:

for each ( var name in firstJsonObject.employees )
{
      System.log("Employee: " + name.firstName + " " + name.lastName);
}

Of course, in the example I kind of skipped the simple usage of an array, just containing strings or numbers. If you would like to build a simpler array, just do it like this:

var firstJsonObject =
{
      "company": "comdivision",
      "address": {
            "country": "Germany",
            "zipcode": "12345",
            "street": "my street",
            "number": "34"
      },
      "servernames": [
            "esxi01",
            "esxi02",
            "esxi03"
      ]
}

Just remember to NOT add a comma at the end of the last element of the array.

I hope this helps a bit understanding the basics of JSON and how to use it in vRealize Orchestrator. Have fun trying it by yourself.