Hi guys, the console is showing me the result "undefined" about the api

RestCountries.eu/rest/v2/capital/tallinn

[
  {
    "name": "Estonia",
    "topLevelDomain": [
      ".ee"
    ],
    "alpha2Code": "EE",
    "alpha3Code": "EST",
    "callingCodes": [
      "372"
    ],
    "capital": "Tallinn",
    "altSpellings": [
      "EE",
      "Eesti",
      "Republic of Estonia",
      "Eesti Vabariik"
    ],
    "region": "Europe",
    "subregion": "Northern Europe",
    "population": 1315944,
    "latlng": [
      59,
      26
    ],
    "demonym": "Estonian",
    "area": 45227,
    "gini": 36,
    "timezones": [
      "UTC+02:00"
    ],
    "borders": [
      "LVA",
      "RUS"
    ],
    "nativeName": "Eesti",
    "numericCode": "233",
    "currencies": [
      {
        "code": "EUR",
        "name": "Euro",
        "symbol": "€"
      }
    ],
    "languages": [
      {
        "iso639_1": "et",
        "iso639_2": "est",
        "name": "Estonian",
        "nativeName": "eesti"
      }
    ],
    "translations": {
      "de": "Estland",
      "es": "Estonia",
      "fr": "Estonie",
      "ja": "エストニア",
      "it": "Estonia",
      "br": "Estônia",
      "pt": "Estónia",
      "nl": "Estland",
      "hr": "Estonija",
      "fa": "استونی"
    },
    "flag": "https://restcountries.eu/data/est.svg",
    "regionalBlocs": [
      {
        "acronym": "EU",
        "name": "European Union",
        "otherAcronyms": [],
        "otherNames": []
      }
    ],
    "cioc": "EST"
  }
]

Your JSON access issue is very similar to this very recent topic below:

So I’m just gonna recapitulate from my previous answer there:

  • The outer datatype of this API JSON stored in variable weather is JSONArray.
  • Inside that we find only 1 big JSONObject entry.
  • That JSONObject contains many properties, and 1 of them is timezones.
  • And that timezones property points to a JSONArray containing 1 String entry, which is “UTC+02:00”.
  • So in order to reach that particular property and its single String content we can go like this:
    weather[0].timezones[0] or weather[0]['timezones'][0]
1 Like