having difficulty parsing json alerts with java script

  • 1
  • Problem
  • Updated 1 year ago

I'm using the javascript bellow on conditions and it works great. but when attempting with alerts I get "undefined"? The only thing I can get to work is "query_zone". I tested with a city that actually has an alert listed. Not sure what I'm doing wrong?


jQuery(document).ready(function ($) { $.ajax({ url: "http://api.wunderground.com/api/key/alerts/q/oh/cleveland.json";, dataType: "jsonp", success: function (parsed_json) { var description = (parsed_json)['description']; var expires = (parsed_json)('expires'); document.getElementById('alert1').innerHTML = (description); document.getElementById('alert2').innerHTML = (expires); } }); });

Photo of dennisman80

dennisman80

  • 2 Posts
  • 0 Reply Likes

Posted 1 year ago

  • 1
Photo of James Anding

James Anding

  • 50 Posts
  • 33 Reply Likes

Try This


   var AlertType       = parsed_json['alerts']['1']['wtype_meteoalarm_name'];  
   var AlertDescription      = parsed_json['alerts']['1']['description']; 
   var AlertDate        = parsed_json['alerts']['1']['date']; 
   var AlertExpires       = parsed_json['alerts']['1']['expires']; 
   var AlertMessage       = parsed_json['alerts']['1']['message']; 
    var level_meteoalarm_description  = parsed_json['alerts']['1']['level_meteoalarm_description'];



Photo of James Anding

James Anding

  • 50 Posts
  • 33 Reply Likes
Sorry forgot to tell you,
The number in the ['1'] is which alert you want to get, as there can be more than one alert.
If there is only one alert it is zero. Hope that helps.
Photo of James Anding

James Anding

  • 2 Posts
  • 1 Reply Like

I would like to clarify how the Alerts API works for users that don’t have a lot of
programming experience. Parsing Jason files is sometimes confusing as they look similar
to one's you may have done before but they are not all handled the same way when trying
to get the data fields from the string.

The following example request is for Alerts from a city.
I used NY,NY because on that day, there were two alerts generated and returned.
This is a good example on parsing the data.

http://api.wunderground.com/api/YourKeyNumber/alerts/q/NY/NY.json

This request returned the following Jason object to the user.

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html";,
  "features": {
  "alerts": 1
  }
 }
  ,"query_zone": "999",
 "alerts": [
  {
  "type": "WND",
  "wtype_meteoalarm": "1",
  "wtype_meteoalarm_name": "Wind",
  "level_meteoalarm": "2",
  "level_meteoalarm_name": "Yellow",
  "level_meteoalarm_description": "The weather is potentially dangerous.",
  "description": "Potential disruption due to wind from 9:40PM CET THU until 11:59PM CET THU",
  "date": "2017-02-23 21:44:02 GMT",
  "date_epoch": "NA",
  "expires": "2017-02-23 22:59:00 GMT",
  "expires_epoch": "NA",
  "message": "Potential disruption due to wind from 9:40PM CET THU until 11:59PM CET THU)",
  "phenomena": "NA",
  "significance": "NA",
  "attribution": "Information provided by - Note: More Text here."
  }
  ,
  {
  "type": "WIN",
  "wtype_meteoalarm": "2",
  "wtype_meteoalarm_name": "Snow/Ice",
  "level_meteoalarm": "2",
  "level_meteoalarm_name": "Yellow",
  "level_meteoalarm_description": "The weather is potentially dangerous.",
  "description": "Potential disruption due to snow and ice from 9:35AM CET SAT until 9:59AM CET SAT",
  "date": "2017-02-25 09:36:01 GMT",
  "date_epoch": "NA",
  "expires": "2017-02-25 08:59:00 GMT",
  "expires_epoch": "NA",
  "message": "Potential disruption due to snow and ice from 9:35AM CET SAT until 9:59AM CET SAT)",
  "phenomena": "NA",
  "significance": "NA",
  "attribution": "Information provided by - Note: More Text here."
  }
 ]
}


If you look at the response returned above, notice that it is basically 2 sections of data.
( Not counting the "response": header)

Both sections contain the same Field Names but have different data.

This response is in the form of an array of data.
There were two Alerts returned and the Jason string has two array members.

The best way to determine if there were zero or many Alerts returned is to find out how many
elements this Response has.

Do this first before attempting to parse the individual data fields.
To get the number of elements in the array do the following:

var ArrayElements = parsed_jason['alerts'];
This gets the entire Alerts object in the variable ArrayElements.

To get how many elements are in the array from this variable, do this:

var NumElements = ArrayElements.length;

Now you have a real number (NumElements) that represents how many Alerts or array elements are in this Jason string.
In our example, this value would be 2. There are two alerts so there are two array elements.
Knowing this, we can now parse the data without our java script blowing up and burning down the computer room.

Note:
If the value of NumElements is 0 ( zero ), there is no need to parse anything. There were no alerts and
thus the size of the array is 0. Attempting to parse an element at this point will only cause your java script
to malfunction. In your program simply display that there are no alerts and move on to the next task.

Using the example we have above:
To get the data from the First Alert, you parse it this way:

var type                        = parsed_jason['alert']['0']['type'];
var wtype_meteoalarm_name       = parsed_jason['alert']['0']['wtype_meteoalarm_name'];
var evel_meteoalarm_descriptio  = parsed_jason['alert']['0']['level_meteoalarm_description'];
var expires                     = parsed_jason['alert']['0']['expires'];

To get the data from the Second Alert, you parse it this way:

var type                        = parsed_jason['alert']['1']['type'];
var wtype_meteoalarm_name       = parsed_jason['alert']['1']['wtype_meteoalarm_name'];
var evel_meteoalarm_descriptio  = parsed_jason['alert']['1']['level_meteoalarm_description'];
var expires                     = parsed_jason['alert']['1']['expires'];

The ['0'] and ['1'] parameters represent which element of the array you want to access.

If there were only one Alert, the var NumElements would be 1 and you access data with the array number of ['0'].
Remember, all elements are zero based.

Since there were only 2 elements in the array ( element 0 and element 1 ) attempting to get data
from element 2 is a bad idea. You must never use an element number that is NOT defined.


(Edited)