Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6895969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:58:30+00:00 2026-05-27T06:58:30+00:00

I’m trying serverside reverse geocoding that can get me a json response and now

  • 0

I’m trying serverside reverse geocoding that can get me a json response and now I want to get 2 or 3 variables from the json response:

I’d like to parse for instance this data and end with eg.
administrative_area_level_1 = 'Stockholm'

jsondata = json.load(urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng=59.3,18.1&sensor=false'))

Here is my python code that fetches the json, now I wonder how to parse it to get just the

  • administrative_area_level_1 long_name (ie state or region name)
  • locality long name (ie city name)
  • an understanding how to parse my json

I can parse it but it is not always is comes out as administrative_area_1:

jsondata["results"][0]["address_components"][5]["long_name"]

The line above correctly outputs “New York” for a point in New York but for Stockholm it outputs a postal city ie Johanneshow which is not the administraive_area_1 (region/state). So how guarantee that the function always returns the administrative_area_1, preferably without looping?

I want it to work something like the following with direct access to country, region and city:

logging.info("country:"+str(jsondata["results"][9]["formatted_address"]))
logging.info("administrative_area_level_1:"+str(jsondata["results"][8]["formatted_address"]))
logging.info("locality:"+str(jsondata["results"][8]["formatted_address"]))

Thanks in advance

Update

It’s a good answer here with the results I expected. While waiting for the answer I also tried implement a solution myself that seems to do it:

jsondata = json.load(urllib2.urlopen('http://maps.googleapis.com/maps/api/geocode/json?latlng='+str(ad.geopt.lat)+','+str(ad.geopt.lon)+'&sensor=false'))
logging.info("geography:"+str(jsondata["results"][1]["formatted_address"]))
region = None
city = None
for result in jsondata["results"]:
  #logging.info("result:"+str(result))
  for component in result["address_components"]:
    logging.info("components:"+str(component))
    logging.info("components type:"+str(component["types"]))
    if 'administrative_area_level_1' in component["types"]:
      #logging.info(unicode('found admin area:%s' % component["long_name"]))
      region = component["long_name"]
    if 'locality' in component["types"]:
      logging.info("found locality:"+str(component["long_name"]))
      city = component["long_name"]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:58:31+00:00Added an answer on May 27, 2026 at 6:58 am

    Processing the response

    There is no need to parse the JSON – it is already parsed by json.load() and returned as Python’s data structure. Use it like simple dictionary with lists or different dictionaries in it.

    Accessing the needed part of the response

    To access data you should be working with you can use the following:

    jsondata['results'][0]['address_components']
    

    which is where all information on geographical names is included:

    [{u’long_name’: u’S\xf6dra L\xe4nken’, u’types’: [u’route’], u’short_name’: u’S\xf6dra L\xe4nken’}, {u’long_name’: u’Stockholm’, u’types’: [u’locality’, u’political’], u’short_name’: u’Stockholm’}, {u’long_name’: u’Stockholm’, u’types’: [u’administrative_area_level_1′, u’political’], u’short_name’: u’Stockholm’}, {u’long_name’: u’Sweden’, u’types’: [u’country’, u’political’], u’short_name’: u’SE’}, {u’long_name’: u’12146′, u’types’: [u’postal_code’], u’short_name’: u’12146′}, {u’long_name’: u’Johanneshov’, u’types’: [u’postal_town’], u’short_name’: u’Johanneshov’}]

    Filtering data you need

    As you can see, there is plenty of data you do not need, but you want only locality and administrative_area_level_1 information. You can filter the data using filter() Python function like that:

    >>> mydata = jsondata['results'][0]['address_components']
    >>> types = ['locality', 'administrative_area_level_1']
    >>> geonames = filter(lambda x: len(set(x['types']).intersection(types)), mydata)
    

    Basically you are getting only elements that have ‘locality’ or ‘administrative_area_level_1’ in their “types” lists. After the above, geonames will be list containing dictionaries you need:

    [{u’long_name’: u’Stockholm’, u’types’: [u’locality’, u’political’], u’short_name’: u’Stockholm’}, {u’long_name’: u’Stockholm’, u’types’: [u’administrative_area_level_1′, u’political’], u’short_name’: u’Stockholm’}]

    Displaying the data

    To display their names you can eg. iterate through them, displaying long_names and respective types values:

    >>> for geoname in geonames:
        common_types = set(geoname['types']).intersection(set(types))
        print '{} ({})'.format(geoname['long_name'], str(', '.join(common_types)))
    
    
    Stockholm (locality)
    Stockholm (administrative_area_level_1)
    

    Is this what you expected?

    Whole code

    The code could look like this:

    import json
    import urllib2
    
    def get_geonames(lat, lng, types):
        url = 'http://maps.googleapis.com/maps/api/geocode/json' + \
                '?latlng={},{}&sensor=false'.format(lat, lng)
        jsondata = json.load(urllib2.urlopen(url))
        address_comps = jsondata['results'][0]['address_components']
        filter_method = lambda x: len(set(x['types']).intersection(types))
        return filter(filter_method, address_comps)
    
    lat, lng = 59.3, 18.1
    types = ['locality', 'administrative_area_level_1']
    
    # Display all geographical names along with their types
    for geoname in get_geonames(lat, lng, types):
        common_types = set(geoname['types']).intersection(set(types))
        print '{} ({})'.format(geoname['long_name'], ', '.join(common_types))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.