Search code examples
javascriptjquerymaxmind

How do I use/store the value of a JSON response?


I'd like to determine what country a visitor of my website is from based off of their geoIP so I can determine whether that visitor is visiting their country-specific website or not and prompt them with a confirmation popup asking if they'd like to be redirected in the event that they're not. However, my JavaScript knowledge is limited/minimal. How would I store/use and return a specific value to be used dynamically from a JSON response?

For example, I'd like to use the following country iso code "US".

"country": {
    "iso_code": "US",

Using MaxMind's GeoIP2 JavaScript Client API with the following JavaScript:

<script type="text/javascript" src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js"></script>

<script type="text/javascript">

var onSuccess = function(location){
  console.log(
  "Lookup successful:\n\n"
  + JSON.stringify(location, undefined, 4)
  );
};

var onError = function(error){
  console.log(
      "Error:\n\n"
      + JSON.stringify(error, undefined, 4)
  );
};

geoip2.city(onSuccess, onError);

</script>

This JSON returned looks like this:

{
  "city": {
    "geoname_id": 5368361,
    "names": {
      "es": "Los Ángeles",
      "fr": "Los Angeles",
      "ja": "ロサンゼルス",
      "pt-BR": "Los Angeles",
      "ru": "Лос-Анджелес",
      "zh-CN": "洛杉矶",
      "de": "Los Angeles",
      "en": "Los Angeles"
    }
  },
  "continent": {
    "code": "NA",
    "geoname_id": 6255149,
    "names": {
      "fr": "Amérique du Nord",
      "ja": "北アメリカ",
      "pt-BR": "América do Norte",
      "ru": "Северная Америка",
      "zh-CN": "北美洲",
      "de": "Nordamerika",
      "en": "North America",
      "es": "Norteamérica"
    }
  },
  "country": {
    "iso_code": "US",
    "geoname_id": 6252001,
    "names": {
      "ru": "США",
      "zh-CN": "美国",
      "de": "USA",
      "en": "United States",
      "es": "Estados Unidos",
      "fr": "États-Unis",
      "ja": "アメリカ合衆国",
      "pt-BR": "Estados Unidos"
    }
  },
  "location": {
    "accuracy_radius": 50,
    "latitude": 34.0669,
    "longitude": -118.3109,
    "metro_code": 803,
    "time_zone": "America/Los_Angeles"
  },
  "postal": {
    "code": "90020"
  },
  "registered_country": {
    "iso_code": "US",
    "geoname_id": 6252001,
    "names": {
      "ja": "アメリカ合衆国",
      "pt-BR": "Estados Unidos",
      "ru": "США",
      "zh-CN": "美国",
      "de": "USA",
      "en": "United States",
      "es": "Estados Unidos",
      "fr": "États-Unis"
    }
  },
  "subdivisions": [
    {
      "iso_code": "CA",
      "geoname_id": 5332921,
      "names": {
        "zh-CN": "加利福尼亚州",
        "de": "Kalifornien",
        "en": "California",
        "es": "California",
        "fr": "Californie",
        "ja": "カリフォルニア州",
        "pt-BR": "Califórnia",
        "ru": "Калифорния"
      }
    }
  ],
  "traits": {
    "autonomous_system_number": 7018,
    "autonomous_system_organization": "AT&T Services, Inc.",
    "isp": "AT&T Services",
    "organization": "AT&T Internet Services",
    "ip_address": "12.125.142.34"
  },
  "represented_country": {
    "names": {}
  }
}

GeoIP2 JavaScript Tutorial

Thanks in advance.


Solution

  • You already have a Json, then you could do something like this:

    var onSuccess = function(location){
      console.log(location.country.iso_code);
    };