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

239
Views
How could I show JSON from url in HTML using Javascript

good afternoon. I'm working on a project where I get a JSON and I have to display it in HTML and I'm not able to do that. In the console I can display all the information correctly, but I cannot display it in the HTML. What do I have to do? Here's the code and thanks for all the help. The error in console is:

Uncaught SyntaxError: Unexpected token o in JSON at position 1
    at JSON.parse (<anonymous>)
    at Object.success (index.html:21:26)
    at i (jquery-3.2.1.min.js:2:28017)
    at Object.fireWith [as resolveWith] (jquery-3.2.1.min.js:2:28783)
    at A (jquery-3.2.1.min.js:4:14035)
    at XMLHttpRequest.<anonymous> (jquery-3.2.1.min.js:4:16323)

And HTML code:

    <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link id="mystylesheet" type="text/css" rel="stylesheet" href="style.css">

  <title>Document</title>
  <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>

<body>
  <div id="wrapper">
    <h1 class="city"></h1>
    <h2><span id="tags">0</span>ºC</h2>
  </div>
  <script>
    const urlJsonString = $.getJSON('https://api.openweathermap.org/data/2.5/onecall?lat=38.7267&lon=-9.1403&exclude=current,hourly,minutely,alerts&units=metric&appid=ecef7e88947b6303121bb900373e41d2', function (data) {
      let urlJson = JSON.parse(urlJsonString)
      let [dt1, dt2, dt3, dt4, dt5, dt6, dt7, dt8] = urlJson.daily.map(({ dt, temp: { day }, weather: [{ description, icon }] }) => ({ dt, day, description, icon }))
      let result = `Data: ${urlJson}`
      $(".city").html(result);
    });
  </script>
</body>

</html>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The returned JSON object is already in the data parameter, so there's no need to parse anything. You could do let urlJson = data, or use data instead of urlJson altogether.

To show the JSON, use stringify, e.g.

var result = `Data: ${JSON.stringify(urlJson)}`;
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!