If you require to decode a base64 string to text, please follow the instructions below:
Illustrated in the image below is an example of a flow where the payload is from a Record Generator source step which decodes the base64 string into text by using JSON code in a Calculator step.
The Record Generator source step contains a payload with a Key and base64 encoded value as shown in the below image.
In the Calculator step, there a function called base64Decode which decodes the base64 string manually using a base64 decoding algorithm and also from the record which contains the base64 string to return the decoded values.
The output of the Calculator step is shown in the image below.
Below is the JSON code that is used in the Calculator step:
function base64Decode(base64) {
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
let decoded = '';
// Remove any characters that are not A-Z, a-z, 0-9, +, /, or =
base64 = base64.replace(/[^A-Za-z0-9+/=]/g, '');
for (let i = 0; i < base64.length;) {
let c1 = chars.indexOf(base64.charAt(i++));
let c2 = chars.indexOf(base64.charAt(i++));
let c3 = chars.indexOf(base64.charAt(i++));
let c4 = chars.indexOf(base64.charAt(i++));
let chr1 = (c1 << 2) | (c2 >> 4);
let chr2 = ((c2 & 15) << 4) | (c3 >> 2);
let chr3 = ((c3 & 3) << 6) | c4;
decoded += String.fromCharCode(chr1);
if (c3 !== 64) {
decoded += String.fromCharCode(chr2);
}
if (c4 !== 64) {
decoded += String.fromCharCode(chr3);
}
}
return decoded;
}
const obj = input.record;
const decodedValue = base64Decode(obj.FileBase64Value);
obj.FileBase64Value = decodedValue;
return obj;