Encoding JSON to Base64


Does anyone knows how I could encode JSON object to base64?

I have seen a few posts but only works for converting strings.

From @emre
function b64(input)
var convert = host.type(“System.Convert”);
var encoding = host.type(“System.Text.Encoding”);
var inputBytes = encoding.UTF8.GetBytes(input);
// return convert.ToBase64String(inputBytes);

return encoding.ToBase64String()

Here is my JSON data:


For all intents and purposes, JSON is a string until it’s deserialised/converted into an object. Passing your JSON data direct to the function should work.

On another note, why convert to Base64?

I passed the JSON Data as string and that worked.

I am required to include a QR Code in all receipts. QR must include: (URL) + (Base64 enconded JSON).

Thank you Memo!

1 Like
let objJsonStr = JSON.stringify(obj);
let objJsonB64 = Buffer.from(objJsonStr).toString("base64");

May be it will help you, obj = your json

so how do i decode from base64 to string ??
base64 to JSON object
thank you in advance

function b64ToString(b64Data) {
    var convert = host.type("System.Convert");
    var encoding = host.type("System.Text.Encoding");
    var bytes = convert.FromBase64String(b64Data);
    var json = encoding.UTF8.GetString(bytes);
    var obj = JSON.parse(json);

that will take a Base64 encoded string (UTF8) and deserialise to obj


@Memo you are a life saver , many thanx

1 Like