Topic
  • 5 replies
  • Latest Post - ‏2013-11-08T16:54:01Z by Dae Young
0RT6_Nilesh_Lambole
0RT6_Nilesh_Lambole
16 Posts

Pinned topic How to encode/decode JSON string with special charactor in Rest request response

‏2013-10-15T12:20:52Z |

Hi All,

We are using rest api in our project for integratiion with .net application.BPM Approval open Tasks are closed using Rest API call with some parameters like comments.

Please suggest how to encode and decode JSON string with special charactor using java script .We tried by encoding JSON string from .net side and while trying to decode using javascript using scriplet with below code:

tw.local.decodedComments = String( Packages.java.lang.String(Packages.org.apache.common.codec.impl.dv.util.Base64.decode(tw.local.encodedComments)));

Giving below error.

TypeError: Cannot call property decode in object [JavaPackage org.apache.common.codec.impl.dv.util.Base64]. It is not a function, it is "object". (#1)

It works fine without any special charactor.I searched on this and found that we need to convert encoded string into byte array then decode it to string.

Please advise how to encode and decode JSON string with special charactors in .net reponse while closing task,we need to save this comments with special charactor in data base.

Thanks :

Nilesh.Lambole

 

 

Updated on 2013-10-15T13:06:06Z at 2013-10-15T13:06:06Z by 0RT6_Nilesh_Lambole
  • kolban
    kolban
    3316 Posts

    Re: How to encode/decode JSON string with special charactor in Rest request response

    ‏2013-10-15T14:13:30Z  

    Would it be possible to post a ZIP file or other mechanism (which won't mangle the data) to show us what the source data looks like?

    Neil

  • Dae Young
    Dae Young
    430 Posts

    Re: How to encode/decode JSON string with special charactor in Rest request response

    ‏2013-10-15T14:13:58Z  

    Good morning Nilesh,

    Please take a look following link in regards to Base64: http://stackoverflow.com/questions/201479/what-is-base-64-encoding-used-for

    When you convert String to byte arrays, you are not decoding it, but encoding it using Base64 encoding scheme. When that object is passed to a remote server using REST WS, the other end (.net application) has to decode it using Base64 so that it could get back original object has been sent by http request from a client machine.

    In regards to above error in red color, you have to call Base64.decode method.

    I think you have to use Java integration service that handles http response coming back as JSON from calling .net application. I believe you can find plenty of good example of how to do it. Basically within a method (refer to below method sample) you have to setRequestProperty method of URL class to setRequestProperty("Accept","application/json") then make sure return value of String (JSON) is wrapped by "(" and ")"  b/c you have to call eval() on returned JSON data in server side script to read curly braces as literal string.

     

    public static String getJSONData(String url) {

    URL url = new URL(url); // location of resource

    HttpURLConection conn = (HttpHURLConnection) urlopenConnection();

    conn.setRequestMethond("GET");

    conn.setRequestType("Accept","application/json");

    ....

    StringBuilder response = new StringBuilder();

    response = newBufferedReader(new InputStreamReader(conn.getInputStream()));

    ....

    return "(" + response.toString() + ")";

     

    }

     

    I know I didn't answer your question in regards to how to encode JSON, but hope I wrote here give you better understanding of how to use Base64 scheme and how to parse JSON data in bpm side.

    Have a blessed day.

    Dae

  • 0RT6_Nilesh_Lambole
    0RT6_Nilesh_Lambole
    16 Posts

    Re: How to encode/decode JSON string with special charactor in Rest request response

    ‏2013-10-16T04:42:26Z  

    Thanks Neil,

    Due to banking project security contraint, I can't provide source data.But it is just the Rest Response with String parameters.Without special charactor it works fine, but with special charactor .net encode this string to base64 but in order to save onto data base from BPM side I am having difficulti while decoding base 64 string.

    Thanks Dae  for your valuable input. Actualy I am facing issue while decoding base64 string provided in .net(encoded) Rest response.

     

    Updated on 2013-10-16T04:42:54Z at 2013-10-16T04:42:54Z by 0RT6_Nilesh_Lambole
  • NT35_Rafael_Castillejo_Asencio
    50 Posts

    Re: How to encode/decode JSON string with special charactor in Rest request response

    ‏2013-11-08T15:58:42Z  

    Thanks Neil,

    Due to banking project security contraint, I can't provide source data.But it is just the Rest Response with String parameters.Without special charactor it works fine, but with special charactor .net encode this string to base64 but in order to save onto data base from BPM side I am having difficulti while decoding base 64 string.

    Thanks Dae  for your valuable input. Actualy I am facing issue while decoding base64 string provided in .net(encoded) Rest response.

     

    Hi NileshLambole,

     

    today I had the same problem that you.  This is the solution that I have done.

    My problem was to recuperate the content of a document attachs to the instance.

    ....

     var documento = bpdDocDelegate.getDocumentById( lastId );
     var tmpFile = Packages.org.apache.xerces.impl.dv.util.Base64.encode(documento.getDocumentData());
     tw.local.contentDocumento = String(Packages.java.lang.String(Packages.org.apache.xerces.impl.dv.util.Base64.decode(tmpFile)));    

    .....

    I hope that this help you

    Regards

    Rafael

  • Dae Young
    Dae Young
    430 Posts

    Re: How to encode/decode JSON string with special charactor in Rest request response

    ‏2013-11-08T16:54:01Z  

    Thanks Neil,

    Due to banking project security contraint, I can't provide source data.But it is just the Rest Response with String parameters.Without special charactor it works fine, but with special charactor .net encode this string to base64 but in order to save onto data base from BPM side I am having difficulti while decoding base 64 string.

    Thanks Dae  for your valuable input. Actualy I am facing issue while decoding base64 string provided in .net(encoded) Rest response.

     

    Hi NileshLambole,

    One more thing I forgot to mention. When encoded value from a service (.net program) gets passed over via http response, make sure to escape reserved characters like "/", "&", "=" and etc via .net version of URLEncoder.decode() function (Java API provides this class). Otherwise those reserved characters won't be decoded correctly from client side. I quickly looked up in google and found HttpUtility.UrlDecode method. By reading its description, "Converts a string that has been encoded (in this case Base64) for transmission in a URL into a decoded string".

    Hope this helps.

    Have a blessed day!

    Dae

    Updated on 2013-11-08T16:55:12Z at 2013-11-08T16:55:12Z by Dae Young