0

I have been using Open Weather and Weather Undground APIs for some time. I recently learned the National Weather Service has an API for alerts. I can print out the raw data but not able to parse it. The JSON format seems a little different then what I am use to.

This is the link to the information I want to parse: https://api.weather.gov/alerts/active?zone=OHC093

Using Arduino_JSON.h I have been doing the following test code to see if I can get the information I would like.

void ParseWeatherAlerts() {

  Serial.println("\t**** Parse Weather Alerts *****");

  JSONVar myObject = JSON.parse(NWS_Current_Severe_Weather_Alerts);

  // JSON.typeof(jsonVar) can be used to get the type of the var
  if (JSON.typeof(myObject) == "undefined") {
    if (flgDebug) {
      Serial.println("\n\tParsing input failed!");
    }
    return;
  }

  String severity = myObject["features"]["severity"];
  String event = myObject["features"]["event"];

  Serial.print("\tSeverity = ");
  Serial.println(severity);

  Serial.print("\tAlert Event = ");
  Serial.println(event);
 
}

I get no information printed out. Been working on this for hours. The API seems to put out some odd data and I can't determine the KEYS for jSON

6
  • Can you describe this "odd data"? Can you show us the rest of the code? Commented Apr 4 at 0:26
  • please do not surround a URL in quotes when posting here Commented Apr 4 at 0:42
  • I get no information printed out ... that makes no sense ... you have several lines that print fixed text ... those should be printing Commented Apr 4 at 0:45
  • 1
    please post code that actually compiles Commented Apr 4 at 0:46
  • where do you get the ["features"]["severity"] in the JSON file? Commented Apr 4 at 0:53

1 Answer 1

1

String severity = myObject["features"]["severity"];

If you run a curl cli command on your PC to the url https://api.weather.gov/alerts/active?zone=OHC093, it returns a json object as shown below (with part of the elements removed to only highlight the structure):

{
    // ...

    "features": [
        {
            // ...

            "properties": {
                // ...

                "severity": "Severe",
                // ...
                "event": "Flood Warning"
                // ...
            },
        },
        
    ],

    // ...

}

Noticed that the features is an array element, so to access it, it is myObject["features"][0]. The "severity" element is an element of "properties". The correct code should therefore be:

JsonDocument doc;

DeserializationError error = deserializeJson(doc, input);

JsonObject myObject = doc["features"][0];
JsonObject propertyObj = myObject["properties"];
const char* severity = propertyObj["severity"];
const char* event = propertyObj["event"];

BTW, ArduinoJson library offers online tool ArduinoJson Assistant for helping you to serialise and deserialise an JSON object.

1
  • 1
    Note that you could have more than one feature (I tried the URL and got two) and, in very clement weather, no features at all. Commented Apr 4 at 9:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.