Five Stars

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Twelve Stars

Re: Decrypt/Decode BASE64 from JSON extract

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.

Rilhia Solutions
2 REPLIES
Twelve Stars

Re: Decrypt/Decode BASE64 from JSON extract

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.

Rilhia Solutions
Five Stars

Re: Decrypt/Decode BASE64 from JSON extract

Oooi...

Yeah, knew I was missing something... Smiley Mad

Thanks again for the help, much appreciated.