Create constant in JS from JSON

Joined
Jul 8, 2022
Messages
3
Reaction score
0
Hello. I have the following JSON data and would like to create a constant in JS contaning the value "17.6", but don't know how to create the data string. I hope somebody can help me. Thanks!


JSON data:

v value:
v 0:
date: 1657274400000
value: "17.6"
quality: "G"
updated: 1657274400000


Javascript:

<script>
fetch(
https://opendata-download-metobs.sm.../1/station/87440/period/latest-hour/data.json
)
.then(response => response.json())
.then(data => {
const tempSpan = document.querySelector("#tempSpan");
tempSpan.innerHTML = data.?????;
});
</script>
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title></title>
    
    <style>
    
    </style>
  
  </head>
  <body>
<span id="tempSpan"></span> 
      <script>
fetch('https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/1/station/87440/period/latest-hour/data.json')
.then(response => response.json())
.then(data => {
console.log(data);
const tempSpan = document.querySelector("#tempSpan"),
      val = data.value[0].value;
      tempSpan.innerHTML = val;
});
</script>
  </body>
</html>
 
Joined
Jul 8, 2022
Messages
3
Reaction score
0
Thanks for your quick response, but unfortunately it's not working. I managed to have another script (below) working perfect, but the JSON content was structured in another way. Now the problem seems to be to pick up the value: "17.6".
I am then using this line to display the value on the web page:
<span id="tempSpan"></span>

<script>
fetch(
"https://opendata-download-metfcst.s...otype/point/lon/17.8684/lat/58.7429/data.json"
)
.then(response => response.json())
.then(data => {
const tempSpan = document.querySelector("#tempSpan");
tempSpan.innerHTML = data.timeSeries[0].parameters[10].values[0];
});
</script>
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
but unfortunately it's not working

Here is a screenshot taken right from my screen a couple of seconds ago. Not working?
 

Attachments

  • val_from_json.jpg
    val_from_json.jpg
    46.6 KB · Views: 9
Joined
Nov 13, 2020
Messages
302
Reaction score
38
@swede, You have modified the code Padonak gave you and that's why it's not working. There is nothing labeled as timeSeries in the JSON datum.
 
Joined
Jul 8, 2022
Messages
3
Reaction score
0
I was not able to view your answers during a couple of days, was waiting for approval from the administrator to read it. In the meantime I found the answer myself. This works. Problem solved.

<script>
fetch('https://opendata-download-metobs.sm.../1/station/87440/period/latest-hour/data.json')
.then(response => response.json())
.then(data => {
console.log(data);
const tempSpan = document.querySelector("#tempSpan");
tempSpan.innerHTML = data.value[0].value
});
</script>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top