Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
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 answers
Answer question

0

Change this

var data = "<%= user %>"

To this:

var data = `<%- user %>`
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!