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

286
Vistas
Not being able to Convert data back to json after using JSON.parse on ejs file using node,express

Im fetching data on my server from a exteranal API, then im trying to send that data to an ejs file using JSON.stringify(data), and trying to read the data in the ejs file with JSON.parse(data), but im either getting something like [Object object] or a string with a lot of weird characters in it, i tried using replace, but it still doesnt work.

This is what i have on the server side.

router.get("/api", (req, res) => {

const info = {
        place: "London",
        country: "UK",
        temp: "16",
        weather: "Cloudy",
      };
      res.render("weathery", { user: JSON.stringify(info) });
}

This is what i have on the ejs file side.

<script>
      var data = "<%= user %>";
      const newData = data;

      console.log(newData);
</script>

This is what it prints without using JSON.parse

{"place":"London","country":"UK","temp":"16","weather":"Cloudy"}

i tried using replace on the weird characters like #,&,; and 34 and it didnt work, also that would lead to a problem on some cases if was receving some data with the number 34

Whenever i tried to parsed it send an error "Uncaught SyntaxError: Unexpected token & in JSON at position 1"

Hopefully you can help me, have a nice day :)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Change this

var data = "<%= user %>"

To this:

var data = `<%- user %>`
about 4 years ago · Juan Pablo Isaza 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