Working with JSON

Some REST clients use XML and some use JSON to exchange data. In this recipe, we will look at how to parse and construct JSON objects in Orchestrator.

Getting ready

You should be comfortable with the recipe JavaScript complex variables in this chapter before starting this recipe.

How to do it...

We will divide this recipe into multiple sections, each of which will deal with the different aspects of JSON.

Parsing JSON REST returns

The (undocumented) JSON.parse function makes an object out of a JSON string. This is typically used with JSON REST returns.

In this example, we look at the REST return from the Control Center GET call: api/server/status.

The method response.contentAsString returns the following string:

{"id":null,"error":null,"warning":null,"requestedStatus":null,"initialStatu
s":"RUNNING","currentStatus":"RUNNING","progress":"Status-ing
tcServer
Instance name: app-server
Runtime version:
8.0.30.C.RELEASE
tc Runtime Base: /var/lib/vco/app-server
Status:
RUNNING as PID=24470
","finished":true}

We are only interested in currentStatus. We can access that part in the following ways:

var jsonObj = JSON.parse(response.contentAsString); 
System.log ("Serverstate = "+ jsonObj.currentStatus); 

Or:

System.log ("Serverstate = "+ jsonObj['currentStatus']); 

Creating a JSON object

When working with REST and JSON, you not only need to know how to parse JSON, but also how to create a JSON object. There is a very good article that shows all the methods: http://www.vcoteam.info/articles/learn-vco/305-creating-json-objects-in-orchestrator.html .

I will quickly show how to do it. You want to create a basic JSON object that looks like this:

{  
"private":"mobile number", 
   "business":"landline number" 
} 

First, we need to build a property with this content:

var propList=new Properties(); 
propList.put("private","mobile number"); 
propList.put("business"," landline number"); 
Then we have to convert it to JSON 
var jsonObj = new Object(); 
for each (key in propList.keys){ 
   jsonObj[key]=propList.get(key); 
} 
Content= JSON.stringify(jsonObj); 

This will create the JSON object string we can use in a REST call. If you need to create more complex JSON with arrays, the same method applies. You construct an array of properties and then stringify it.

Change JSON object

We also have the ability to alter JSON objects quite easily. A typical example is that you have a REST GET that results in a JSON and you like to PUT the same back with some slide changes. Here is how that works:

getContent= REST.getContentAsMimeAttachment(); 
jsonObj = JSON.parse(getContent.content); 
jsonObj['private'] = "another number"; 
putContent= JSON.stringify(jsonObj); 

How it works...

Orchestrator uses the JSON data interchange standard to exchange information via the REST API. Some other REST APIs (such as vCloud Director) use XML. JSON stands for JavaScript Object Notation and is a standard for exchanging information.

JSO knows the following elements:

  • Number: A simple number with the English separation of a dot between full numbers and fractions
  • Value: A value can be a string, number, object, array, true, false, and null

We can also create more complex types, such as objects and arrays.

A JSON object is constructed as follows:

Format

{ string : value } or {string1:value1, string2:value2}

Example

{"version":"7.0.1", "Build Number":3232}

An array in JSON is represented as follows:

Format

[value1,value2]

Example

["Mother","Father"] or [{"Mother":"Mary", "Father":"Adam"]}

JSON is quite simple when you get the hang of it. If you need to construct JSON you are mostly working with objects, properties, and arrays. All these are explained in the recipe JavaScript complex  variables  in this chapter.

See also

See the example workflow 07.04.1 Orchestrator Service Status.

More information on JSON can be found here: http://www.json.org/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset