I have a object uriBase:
const nameNFT = `...`;
const descriptionNFT = `...`;
const svg = `...`;
const uriBase = {
Name: `${nameNFT}`,
Description: `${descriptionNFT}`,
Painting: `${svg}`,
};
How can I encode and decode a objedct to Base64?
I use ReactJs, I`m tryng something like this:
$ npm install --save js-base64
import { Base64 } from "js-base64";
const uriBase64 = Base64.encode(uriBase);
return uriBase64;
and
const decodeBase64Uri= Base64.decode(uriBase);
return decodeBase64Uri;
and in the console
console.log(decodeBase64Uri);
print:
[Object object]
>Solution :
Use Buffer.
const object = {
foo: 'bar',
};
// convert it to base64
const base64 = Buffer.from(JSON.stringify(object)).toString('base64');
console.table({ base64 });
// convert it back
const back = JSON.parse(Buffer.from(base64, 'base64').toString('utf-8'));
console.log(back);