How to build JSON with JavaScript vR Orchestrator

How to build JSON with JavaScript vR Orchestrator

Introduction to JSON

JSON = JavaScript Object Notation

The JSON syntax is derived from JavaScript object notation syntax:

curly Braces hold objects { <object> }
Data is in name/value pairs"name":"value"
Data is separated by commas<data>, <data>
Square brackets hold arrays"arrayname" [ <data> ]

Example:

{
  "Name": "VM01",
  "ID": "jdhh456h5h3",
  "OS": {
    "OSType": "Linux",
    "OSVendor": "Ubuntu",
    "OSVersion": "17.04"
  },
  "Hardware": [
    {
      "CPUVendor": "Intel",
      "CPUcores": "4"
    },
    {
      "MEMmb": "1024"
    }
  ]
}
2,17 : name/value

The Object named “Name” (line 2) can only exits once.

3-7  : object with name/value items

The Object named “OS” (line 3) is a nested object, and has several Propertys (line 4-6).

8-16 : array of objects

The Array named “Hardware” (line 8) has multiple Propertyobjects (line 9-15).

Now we build this JSON with JavaScript in vRO

Build JSON body and add properties

Start with creating a new vRO workflow, and add a scriptable element.

Basic vRO workflow, with scriptable element

And add the following code.

localJSON = new Object();

localJSON.Name = "VM01";
localJSON.ID = "jdhh456h5h3";

System.log(JSON.stringify(localJSON));

First we create the base JavaScript object for JSON (line 1). And with then we add the Name (line 3) and ID (line 4) property to base object.

As last step we convert with “JSON.stringify()” the JavaScript object to a simple string, and write it out to the System.log.

output:

{"Name":"VM01","ID":"jdhh456h5h3"}

Prettified JSON

{
  "Name":"VM01",
  "ID":"jdhh456h5h3"
}

Add the nested “OS” property object

Now add the highlighted lines in your existing code.

localJSON = new Object();

localJSON.Name = "VM01";
localJSON.ID = "jdhh456h5h3";

os = new Object();

os.Type = "Linux";
os.Vendor = "Ubuntu";
os.Version = "17.04";

localJSON.OS = os;

System.log(JSON.stringify(localJSON));

First we add JavaScript object “OS” (line 5), and add Type, Vendor and Version as properties (line 7-9).

And then we add the “OS” as property to “localJSON.OS” (line 11).

output:

{"Name":"VM01","ID":"jdhh456h5h3","OS":{"type":"Linux","Vendor":"Ubuntu","Version":"17.04"}}

Prettified JSON

{
  "Name":"VM01",
  "ID":"jdhh456h5h3",
  "OS":{
    "type":"Linux",
    "Vendor":"Ubuntu",
    "Version":"17.04"
  }
}

Add the the Array object “Hardware”

Just add the highlited JavaScript code lines.

localJSON = new Object();

localJSON.Name = "VM01";
localJSON.ID = "jdhh456h5h3";

os = new Object();

os.type = "Linux";
os.Vendor = "Ubuntu";
os.Version = "17.04";

localJSON.OS = os;

hardware = new Array();
cpu = new Object();
mem = new Object()

cpu.CPUVendor = "Intel";
cpu.CPUcores = "4";

mem.MEMmb = "1024";

hardware.push(cpu);
hardware.push(mem);

localJSON.Hardware = hardware;

System.log(JSON.stringify(localJSON));

We create the array “hardware” (line 14), and then the objects for cpu and men (line 15,16). Then add the properties to cpu and men (line 18-21). We use the “push” method to add cpu and mem to the Array (line 23,24.

As last step we need to add the array “hardware” as property to the JSON base object (line 26).

output

{"Name":"VM01","ID":"jdhh456h5h3","OS":{"type":"Linux","Vendor":"Ubuntu","Version":"17.04"},"Hardware":[{"CPUVendor":"Intel","CPUcores":"4"},{"MEMmb":"1024"}]}

Prettified JSON

{
  "Name": "VM01",
  "ID": "jdhh456h5h3",
  "OS": {
    "type": "Linux",
    "Vendor": "Ubuntu",
    "Version": "17.04"
  },
  "Hardware": [
    {
      "CPUVendor": "Intel",
      "CPUcores": "4"
    },
    {
      "MEMmb": "1024"
    }
  ]
}

Building JSON with JavaScript is at the end very simple, but you should always work with the objects. And do NOT build JSON in the plain text way, because this is very error prone.

In one of my next blog posts, i will walk you through the different ways to extract, change and extend data from JSON.

Privacy Preference Center

    Necessary

    Advertising

    Analytics

    Other