Wednesday, October 2, 2013

How to Convert TWObject to JSON


JSON has been very famous now a days, especially while using REST.
Here is the script to create a JSON out of a teamworks object.

Say I have tw object of type EMPLOYEE with the parameters (name, place,phone). After executing the script below it will give the JSON as below.

statement:
tw.local.json="{\"Employee\":"+createJson(tw.local.employee)+"\""};

result :

tw.local.json  will have the value {"Employee":{"name":"abcd","place":"ongole","phone":"xxxxxx"}}
 



function createJson(twObject){
 
    var jsonString="";
    if(typeof twObject =='object' && twObject!=null){
        if(twObject.listLength>0){
            jsonString+="[";
         
            for (var j=0; j<twObject.listLength; j++){
           
    if(typeof twObject[j]!='string')
              jsonString+="{";

            for (var property in twObject[j].propertyNames){
                 
                   var name = twObject[j].propertyNames[property];
                   if(typeof twObject[j][name]=='object'){
                       if(Object.prototype.toString.call(twObject[j][name]).indexOf("TWDate")!="-1"){
                           jsonString+="\""+name+"\":\""+twObject[j][name].format("yyyy-MM-dd'T'HH:mm:ss'Z'")+"\",";
                       }else{
                           jsonString+="\""+name+"\":"+createJson(twObject[j][name]);
                       }                    
                   }
                   else{
                       jsonString+="\""+name+"\":\""+twObject[j][name]+"\",";
                   }
                 
                   if(twObject[j].listLength>0)
                   {
                     for (var k=0;k<twObject[j].listLength;k++){
                     jsonString+="\""+ twObject[j][k]+"\",";
                     }
                   }
           
            }
               if(typeof twObject[j] == 'string'){
               jsonString+="\""+twObject[j]+"\"";
             
                }
       
             if(typeof twObject[j]!='string')
              jsonString+="}";
     
       
         
         
           if(j!=twObject.listLength-1){
               jsonString+=",";
           }
}    jsonString+="],";
        }else{
            try{
            if(twObject.propertyNames.length>0){
            jsonString+="{";
             for(var property in twObject.propertyNames){
                 
                   var name = twObject.propertyNames[property];
                   if(typeof twObject[name]=='object'){
                       if(Object.prototype.toString.call(twObject[name]).indexOf("TWDate")!="-1"){
                           jsonString+="\""+name+"\":\""+twObject[name].format("yyyy-MM-dd'T'HH:mm:ss'Z'")+"\",";
                       }else{
                           jsonString+="\""+name+"\":"+createJson(twObject[name]);
                       }
                   
                   }else{
                       jsonString+="\""+name+"\":\""+twObject[name]+"\",";
                   }
                 
             
                }
             jsonString+="},"; }
             else{
                 return "{},";
             }}catch(e){
                 return "{},";
             }
        }
     
  }
  else if(typeof twObject =='object' && twObject==null){
        return "{},";
    }
  return jsonString;
}

Hope this helpful.

Thanks for stopping by. If you like this post leave a comment, share it and do a g +1.



10 comments:

  1. does this works if your Employee TW complex obj has one or more complex objs inside it?

    ReplyDelete
  2. I have tried with one level of complex object.I worked for me ... please try with multi level and check.

    ReplyDelete
  3. This doesn't seem to work for me. It just outputs {}

    This is my Object that I am passing:

    <_object type="ParentObject">
    test
    test

    test
    test

    true
    false


    2064.bddbc395-4820-4644-b774-255d6deb6d34T
    ChildObject



    true
    false


    2064.5edc8e67-da17-4f43-b8da-2e7e705dbcd1T
    ParentObject

    ReplyDelete
  4. If found the error. The code you posted uses a method called "convertToJson" whereas you probably meant to use your same method (to call it recursively) "createJson." Once I replaced both calls to convertToJson() with createJson() your code seems to work perfectly. Thanks so much for posting this!

    ReplyDelete
  5. Thanks for finding this Dominic. I have updated the script now.

    ReplyDelete
    Replies
    1. Hi,
      I tried this and not able to get success I got an run time error and I am using IBM BPM 8.5.5 and please let me know hoe to create a twObject to JSON.

      Delete
    2. I tried this in my system and getting runtime error do I need to download or do anything like sever files like that to run this code because my requirement is generate JSON object from a TW business object can anyone help me on this.

      Delete
  6. Hi Priya.. Thanks for stopping by. This is an example which was executed in v8.0, not sure if there is some APIS changed in v8.5.5. Will have to check and get back to you.

    ReplyDelete
  7. I wrote TWObject to/from JSON converter as part of an integration tool (https://github.com/valentine20xx/ibbpmintegration). So you can try that, but it has some limitations.

    ReplyDelete
  8. Thanks for the input Nick.This may help many people who is trying to control TWObject to/from JSON.

    ReplyDelete