0

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]

1 Answers1

0

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);
mstephen19
  • 1,733
  • 1
  • 5
  • 20
  • @brunovjk `console.table` will save your life sometimes haha. Also check out `console.dir` and `console.time` + `console.timeEnd` – mstephen19 Apr 19 '22 at 17:58