Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

363
Vistas
Encoding Text to Base64 and decoding back to JSON

I am trying to encode a text to Base64 and using NodeJS, and then while getting the data I am decoding it back from Base64. Now I need to change the data into JSON so I could fill the relevant fields but its not converting to JSON.

Here is my code:

fs.readFile('./setting.txt', 'utf8', function(err, data) {
    if (err) throw err;
    var encodedData = base64.encode(data);

    var decoded = base64.decode(encodedData).replace(/\n/g, '').replace(/\s/g, " ");
    
   return res.status(200).send(decoded);

    });

In setting.txt I have the following text:

LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1

This decodes the result properly but when I use JSON.parse(JSON.stringify(decoded ) its not converting to JSON.

Can someone help me with it.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

What does base-64 encoding/decoding have to do with mapping a list of tuples (key/value pairs) like this:

LENGTH=1076
CRC16=28653
OFFSET=37
MEASUREMENT_SAMPLING_RATE=4
MEASUREMENT_RANGE=1
MEASUREMENT_TRACE_LENGTH=16384
MEASUREMENT_PRETRIGGER_LENGTH=0
MEASUREMENT_UNIT=2
MEASUREMENT_OFFSET_REMOVER=1

into JSON?

If you want to "turn it (the above) into JSON", you need to:

  • Decide on what its JSON representation should be, then
  • Parse it into its component bits, convert that into an appropriate data struct, and then
  • use JSON.stringify() to convert it to JSON.

For instance:

function jsonify( document ) {
  const tuples = document
                 .split( /\n|\r\n?/ )
                 .map( x => x.split( '=', 2) )
                 .map( ([k,v]) => {
                   const n = Number(n);
                   return [ k , n === NaN ? v : n ];
                 });
  const obj = Object.fromEntries(tuples);

  const json = JSON.stringify(obj);
  return json;
}
over 4 years ago · Santiago Trujillo Denunciar

0

Try below snippet

let base64Json=  new Buffer(JSON.stringify({}),"base64").toString('base64');
let json = new Buffer(base64Json, 'ascii').toString('ascii');
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda