0

I'm attempting to access some data from a JSON structure returned from the MesoWest API service for weather data. Currently, I'm just trying to access the latest observation of snow depth at Mt. Baker Ski Area, eventually I'd like to make the request more complicated and plot timeseries' of data. I figured that simply starting with the latest observations from one sensor would be the best place to refine my syntax, but am having trouble accessing the particular value in the object. Can anyone offer me any clarification on these methods? Thank you!

My Javascript follows, I've been testing it in a lite manner using JSBin.com:

async function getObs() {
    try {
        let response = await fetch('http://api.mesowest.net/v2/stations/nearesttime?&stid=mtb42&units=ENGLISH&token=804c1eb09a394255beef8e84b68123bf');
        if (response.ok) {
            let jsonData = await response.json();
            //console.log(jsonData);
            let obs = jsonData.STATION;
            console.log(obs);

        }
    throw new Error('Request Failed!');
    } catch (error) {
        //console.log(error);
    }
}

getObs();

which works, only it returns the whole object. When I attempt to access a particular value, like:

async function getObs() {
    try {
        let response = await fetch('http://api.mesowest.net/v2/stations/nearesttime?&stid=mtb42&units=ENGLISH&token=804c1eb09a394255beef8e84b68123bf');
        if (response.ok) {
            let jsonData = await response.json();
            //console.log(jsonData);
            let obs = jsonData.STATION.OBSERVATIONS.snow_depth_value_1;
            console.log(obs);

        }
    throw new Error('Request Failed!');
    } catch (error) {
        //console.log(error);
    }
}

getObs();

it does not work. Any insights into this?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Will Howard
  • 25
  • 1
  • 7
  • "it does not work" is not an error description. – Tomalak Jan 04 '18 at 05:45
  • any `console` errors in yout application? – Imesh Chandrasiri Jan 04 '18 at 05:46
  • Why does it not work? – Chris Jan 04 '18 at 05:47
  • Hard to answer on this forum without any details of what is going wrong. I suggest you use the explorer on the MesoWest website to verify that you can obtain useful data. Check the reply you receive and confirm it does **not** contain a field " "RESPONSE_MESSAGE": "No stations found for this request." – traktor Jan 04 '18 at 06:30
  • @Tomalak my apologies, I am returned this error in the console: Blocked loading mixed active content “http://api.mesowest.net/v2/stations/nearesttime?&stid=mtb42&units=ENGLISH&token=804c1eb09a394255beef8e84b68123bf” [Learn More] asyncAPI_test.js:5:23 – Will Howard Jan 04 '18 at 22:37
  • @ImeshChandrasiri I receive a 'Blocked loading mixed active content' message. This has to do with security protocols if I'm not mistaken? – Will Howard Jan 04 '18 at 22:39
  • Try going through the following post : https://stackoverflow.com/questions/18251128/why-am-i-suddenly-getting-a-blocked-loading-mixed-active-content-issue-in-fire – Imesh Chandrasiri Jan 05 '18 at 05:40

1 Answers1

1

This is the right way to access this object:

...    
let obs = jsonData.STATION[0].OBSERVATIONS.snow_depth_value_1;
...

Station is an array.