Using JSON

JSON is a string representation of a JavaScript object that can be transmitted over the network. You can use it to pass parameters inside IBM® BPM.

About this task

An Open Source JavaScript implementation is available to both parse and construct JSON strings. Part of this package is a JavaScript source file called json2.js.

Procedure

Add this file to your toolkit or process application as a server managed file to provide new global methods and objects.

Example

This example illustrates how to taking a List of Complex data structure and building a JSON representation:
var newArray = new Array();
for (var j=0; j<tw.local.myPurchase.listLength; j++)
{
  var newObj = new Object();
  for (var property in tw.local.myPurchase[j].propertyNames)
  {
    var name = tw.local.myPurchase[j].propertyNames[property];
    newObj[name] = tw.local.myPurchase[j][name];
  }
  newArray.push(newObj);
}
var jsonText = JSON.stringify(newArray);
log.error("jsonText = " + jsonText);
Within a browser or the coach, you can use the Dojo classes to work with JSON:
  • dojo.fromJson(string) parses a JSON string to return a JavaScript object.
  • dojo.toJson(Object) returns a JSON string given a JavaScript object.