Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I encode and decode a objedct to Base64?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading