Identify number of elements in a JSON key

Hi, I have a JSON output from openweather API. I want to be able to determine how many different alerts are present (See alerts section in bold). I have tried this but the answer is 0. Thanks advance!

  JsonObject alerts = doc["alerts"];
  Serial.print("# de elements in alerts:"); 
  Serial.println(alerts.size());

The JSON output from the API:

{"lat":18.4274,"lon":-67.1541,"timezone":"America/Puerto_Rico","timezone_offset":-14400,"current":{"dt":1662735043,"sunrise":1662718534,"sunset":1662763001,"temp":89.62,"feels_like":102.22,"pressure":1013,"humidity":69,"dew_point":78.08,"uvi":9.89,"clouds":43,"visibility":10000,"wind_speed":3.62,"wind_deg":55,"wind_gust":6.51,"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}]},"alerts":[{"sender_name":"NWS San Juan (San Juan, Puerto Rico)","event":"Rip Current Statement","start":1662686040,"end":1662760800,"description":"...HIGH RIP CURRENT RISK REMAINS IN EFFECT THROUGH FRIDAY\nAFTERNOON...\n* WHAT...Dangerous rip currents expected due to breaking waves to\naround 6 feet.\n* WHERE...Beaches of northwestern Puerto Rico.\n* WHEN...Through Friday afternoon.\n* IMPACTS...Dangerous swimming conditions. Rip currents can\nsweep even the best swimmers away from shore into deeper\nwater.","tags":[]},{"sender_name":"NWS San Juan (San Juan, Puerto Rico)","event":"Heat Advisory","start":1662732000,"end":1662753600,"description":"...HEAT ADVISORY IN EFFECT FROM 10 AM THIS MORNING TO 4 PM AST\nTHIS AFTERNOON...\n* WHAT...Excessive heat with heat indices of 102-109 degrees\nexpected.\n* WHERE...Some lower elevations of northern-central, northwest and\nwest Puerto Rico.\n* WHEN...Between 10 AM and 4 PM AST this afternoon.\n* IMPACTS...High temperatures and humidity may cause heat\nillnesses to occur. Heat exhaustion likely with prolonged\nexposure. Heat stroke possible.","tags":["Extreme temperature value"]}]}
Pretty:
{
  "lat": 18.4274,
  "lon": -67.1541,
  "timezone": "America/Puerto_Rico",
  "timezone_offset": -14400,
  "current": {
    "dt": 1662735043,
    "sunrise": 1662718534,
    "sunset": 1662763001,
    "temp": 89.62,
    "feels_like": 102.22,
    "pressure": 1013,
    "humidity": 69,
    "dew_point": 78.08,
    "uvi": 9.89,
    "clouds": 43,
    "visibility": 10000,
    "wind_speed": 3.62,
    "wind_deg": 55,
    "wind_gust": 6.51,
    "weather": [
      {
        "id": 802,
        "main": "Clouds",
        "description": "scattered clouds",
        "icon": "03d"
      }
    ]
  },
  "**alerts"**: [
    {
      "sender_name": "NWS San Juan (San Juan, Puerto Rico)",
      "event": "Rip Current Statement",
      "start": 1662686040,
      "end": 1662760800,
      "description": "...HIGH RIP CURRENT RISK REMAINS IN EFFECT THROUGH FRIDAY\nAFTERNOON...\n* WHAT...Dangerous rip currents expected due to breaking waves to\naround 6 feet.\n* WHERE...Beaches of northwestern Puerto Rico.\n* WHEN...Through Friday afternoon.\n* IMPACTS...Dangerous swimming conditions. Rip currents can\nsweep even the best swimmers away from shore into deeper\nwater.",
      "tags": []
    },
    {
      "sender_name": "NWS San Juan (San Juan, Puerto Rico)",
      "event": "Heat Advisory",
      "start": 1662732000,
      "end": 1662753600,
      "description": "...HEAT ADVISORY IN EFFECT FROM 10 AM THIS MORNING TO 4 PM AST\nTHIS AFTERNOON...\n* WHAT...Excessive heat with heat indices of 102-109 degrees\nexpected.\n* WHERE...Some lower elevations of northern-central, northwest and\nwest Puerto Rico.\n* WHEN...Between 10 AM and 4 PM AST this afternoon.\n* IMPACTS...High temperatures and humidity may cause heat\nillnesses to occur. Heat exhaustion likely with prolonged\nexposure. Heat stroke possible.",
      "tags": [
        "Extreme temperature value"
      ]
    }
  ]
}

Welcome

Try:

JsonArray alerts = doc["alerts"].as<JsonArray>();
Serial.print("# de elements in alerts:"); 
Serial.println(alerts.size());

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.