Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Decrypt/Decode BASE64 from JSON extract

Hi All,

I am currently busy with a project to retrieve data from a third party website that we use to conduct surveys. The site uses a Remote control panel as an API and from there you can collect the data that you require.

One of the functions is a 'export_responses' call. However, this particular call send the results back as a BASE64 encoded string. I can get the actual response from the JSON feed via XPATH, but I need to decode the BASE64 string to be of actual use.

Any idea on how I can do this?

Currently, after I have used tExtractJSON to get the string I tried;

byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(row6.Body);
System.out.println("Decrypted survey responses: " + buf);
row7.Body = buf;

in a tJava component just to see if I can actually see what the responses are. But I get:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from byte[] to String

Ok, type mismatch, but what should the input flow then be or how do I need to change the decoding to get the actual info.

The string comes in as: (Only a small portion of the extract is below, adding the full extract gives me a warning that the post should not exceed 20 000 characters, so yeah, a rather large string...)

Spoiler
{"id":1, "result":"ImlkIiwic3VibWl0ZGF0ZSIsImxhc3RwYWdlIiwic3RhcnRsYW5ndWFnZSIsInRva2VuIiwic3RhcnRkYXRlIiwiZGF0ZXN0YW1wIiwiaXBhZGRyIiwicmVmdXJsIiwiQTAxW1NRMDAxXSIsIkEwMiIsIkEwM1tTUTAwMV0iLCJBMDQiLCJBMDUiCiIxIiwiMjAxNS0wNS0xNSAxMToxOTowNCIsIjEiLCJlbiIsIj=", "error":null}

 Now from tExtractJSON I can get the response, no issue, but how do I decode this?

(I might be missing something simple, but that is on me and the fact that I have been working on this solid for the past day. A bit of an insomniac hahahah)

Labels (4)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

This code appears to work.....I just tested it and it converted your data into something meaningful 🙂

 

String body = "ImlkIiwic3VibWl0ZGF0ZSIsImxhc3RwYWdlIiwic3RhcnRsYW5ndWFnZSIsInRva2VuIiwic3RhcnRkYXRlIiwiZGF0ZXN0YW1wIiwiaXBhZGRyIiwicmVmdXJsIiwiQTAxW1NRMDAxXSIsIkEwMiIsIkEwM1tTUTAwMV0iLCJBMDQiLCJBMDUiCiIxIiwiMjAxNS0wNS0xNSAxMToxOTowNCIsIjEiLCJlbiIsIj==";

 

byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(body);

 

String str = new String(buf, java.nio.charset.StandardCharsets.UTF_8);

 

System.out.println(str);

 

You were just missing the code to convert your byte array into a String.

View solution in original post

4 Replies
Anonymous
Not applicable
Author

This code appears to work.....I just tested it and it converted your data into something meaningful 🙂

 

String body = "ImlkIiwic3VibWl0ZGF0ZSIsImxhc3RwYWdlIiwic3RhcnRsYW5ndWFnZSIsInRva2VuIiwic3RhcnRkYXRlIiwiZGF0ZXN0YW1wIiwiaXBhZGRyIiwicmVmdXJsIiwiQTAxW1NRMDAxXSIsIkEwMiIsIkEwM1tTUTAwMV0iLCJBMDQiLCJBMDUiCiIxIiwiMjAxNS0wNS0xNSAxMToxOTowNCIsIjEiLCJlbiIsIj==";

 

byte[] buf = new sun.misc.BASE64Decoder().decodeBuffer(body);

 

String str = new String(buf, java.nio.charset.StandardCharsets.UTF_8);

 

System.out.println(str);

 

You were just missing the code to convert your byte array into a String.

Anonymous
Not applicable
Author

Oooi...

Yeah, knew I was missing something... 0683p000009MAqO.png

Thanks again for the help, much appreciated.

DBLONDEL1643728674
Contributor III
Contributor III

Hello!

 

I did the same code, but the file oesn't decode properly, i get an empty pdf

would you help me please

thanks

Anonymous
Not applicable
Author

Hi @Damien BLONDEL​ ,

 

Sorry about the late reply. I have only just got back from holiday. It appears that there were two problems with the above....

 

  1. A final "=" was missing on the encrypted String to be decrypted. This *may* have happened during our migration of posts from one environment to another. I have edited this to correct it.
  2. The code involving sun.misc.Base64Decoder() has been deprecated. As such, a slightly different method is required. I have built some replacement code and you can see this below.....

 

String body = "ImlkIiwic3VibWl0ZGF0ZSIsImxhc3RwYWdlIiwic3RhcnRsYW5ndWFnZSIsInRva2VuIiwic3RhcnRkYXRlIiwiZGF0ZXN0YW1wIiwiaXBhZGRyIiwicmVmdXJsIiwiQTAxW1NRMDAxXSIsIkEwMiIsIkEwM1tTUTAwMV0iLCJBMDQiLCJBMDUiCiIxIiwiMjAxNS0wNS0xNSAxMToxOTowNCIsIjEiLCJlbiIsIj==";

 

java.util.Base64.Decoder decoder = java.util.Base64.getDecoder();

byte[] buf = decoder.decode(body.getBytes(java.nio.charset.StandardCharsets.UTF_8));

 

String str = new String(buf, java.nio.charset.StandardCharsets.UTF_8);

 

System.out.println(str);

 

Let me know if this helps.

 

Regards

 

Richard