Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

289
Visualizações
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 Respostas
Responde à pergunta

0

Change this

var data = "<%= user %>"

To this:

var data = `<%- user %>`
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda