Auto Conversion of BPM TwObject to Json and Json to TwObject

Mostly in any development project we are using json for the data interchanging. Let it be with in the application or calling any external application. It is no difference in BPM .If there is any need to call external system it would be easy to use json rather than Tw objects,XML and vice versa for external systems to call BPM.

If you are using BPM 8.6 version there is an JSAPI  available for conversion of TWObject to JSON

Check Here

toJSONString( boolean formatted) Returns the JSON representation of the object

tw.local.json = tw.local.data.toJSONString(true);

But this api may not work for the lower BPM versions. Moreover there is no JSAPI available to convert JSON object to TWObject.

To solve this problem we can use BPM JSON helper toolkit to convert TWObjects to JSON and JSON to TwObjects and it is based on json2.js To keep our Process App size down,instead of entire helper kit we can just add 2 files to server as dependencies.

json2.js

BPM-JSON-Utils.js
Can be downloaded here : Git Download

server

Once we add these files now we can easily do what we required.

To convert TwObject to Json we need to call function convertTwToJSON(object) which is in Utils.js file

tw.local.json = BPMJSON.convertTwToJSON(tw.local.data);

Similarly to convert JSON to Twobject we use the below function

tw.local.data = BPMJSON.convertJSONToTw(tw.local.json);

Below is one sample example:

Initially I’m creating two integration services

service1

Here json variable is the output variable and data is the input variable to that service

service2

Here data is the output variable and json is the input variable to this service.
Then Ive created a simple human service so that I will call these services.

coach

Human service Diagram

 

coachoutput

Human service output

Here on click of convert to json the , JSONConvertor service is called and it returned the json. Same way we can use the json and convert to UI and directly bind that to UI.

NOTE : The point to be noted while converting json to Twobject , the output Tw object should exactly match with json structure or else it will throw an error.

This way we can easily work with Json even for complex business objects in bpm.

Hope it helps someone !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s