Difficulty accessing information in api's returned json array. What am I doing wrong?

P

Patrick

Guest
I am trying to get information out of a json response from the weather.gov api (https://api.weather.gov/). There's a good chance that I'm overthinking my problem, because I am getting the expected number of responses, but all 2,748 of those responses are 'undefined'.

I've tried accessing the properties that I have seen in the json object response via dot notation, I have tried .map and .forEach methods to possibly try to get a different output, and I have read several dozen blog posts that seem to deal with "simpler" api's. I have also read several posts here on SO, but I have not been able to understand what's wrong.

I'm pretty sure at this point that my mistake is small, but I can't see it.


const endpoint = 'https://api.weather.gov/stations/';

async function getStations() {
const response = await fetch(endpoint);
const data = await response.json();

stationInfo = data.observationStations;
// data.observationStations.forEach(function(e) {
// console.log(e.properties);
// });

for(let i in stationInfo) {
// ext = stationInfo.slice(-4);
// console.log(endpoint + ext);
console.log(stationInfo.id)
}
}

getStations();



here is an example of the json that I'm trying to work with

{
"@context": [
"https://raw.githubusercontent.com/geojson/geojson-ld/master/contexts/geojson-base.jsonld",
{
"wx": "https://api.weather.gov/ontology#",
"s": "https://schema.org/",
"geo": "http://www.opengis.net/ont/geosparql#",
"unit": "http://codes.wmo.int/common/unit/",
"@vocab": "https://api.weather.gov/ontology#",
"geometry": {
"@id": "s:GeoCoordinates",
"@type": "geo:wktLiteral"
},
"city": "s:addressLocality",
"state": "s:addressRegion",
"distance": {
"@id": "s:Distance",
"@type": "s:QuantitativeValue"
},
"bearing": {
"@type": "s:QuantitativeValue"
},
"value": {
"@id": "s:value"
},
"unitCode": {
"@id": "s:unitCode",
"@type": "@id"
},
"forecastOffice": {
"@type": "@id"
},
"forecastGridData": {
"@type": "@id"
},
"publicZone": {
"@type": "@id"
},
"county": {
"@type": "@id"
}
}
],
"id": "https://api.weather.gov/stations/KVBS",
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-93.629999999999995,
29.48
]
},
"properties": {
"@id": "https://api.weather.gov/stations/KVBS",
"@type": "wx:ObservationStation",
"elevation": {
"value": 68.275199999999998,
"unitCode": "unit:m"
},
"stationIdentifier": "KVBS",
"name": "SABINE 13B OIL PLATFORM",
"timeZone": "America/Chicago",
"forecast": "https://api.weather.gov/zones/forecast/GMZ450"
}
}



Since I was successful in getting the url's for each item in the json array, I expected to be able to access the properties of those items also - instead, I'm getting 'undefined'. However as I said earlier, I'm getting 'undefined' the expected number of times.

Continue reading...
 
Top