Reputation: 25
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?
Upvotes: 0
Views: 3507
Reputation: 67
This is the right way to access this object:
...
let obs = jsonData.STATION[0].OBSERVATIONS.snow_depth_value_1;
...
Station is an array.
Upvotes: 1