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):
If you would like to access the information simply use:
The prints the value into the log output of vRO.
JSON objects are highly flexible. You can always add additional information to the object, once created.
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:
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:'
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:
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.
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:
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:
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.