Encoding JSON to Base64

Hello,

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:

{“ver”:1,“fecha”:“2020-10-13”,“cuit”:30000000007,“ptoVta”:10,“tipoCmp”:1,“nroCmp”:94,“importe”:12100,“moneda”:“DOL”,“ctz”:65,“tipoDocRec”:80,“nroDocRec”:20000000001,“tipoCodAut”:“E”,“codAut”:70417054367476}

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

hello
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

3 Likes

@Memo you are a life saver , many thanx

1 Like