Karthik SP Karthik SP - 5 months ago 22
JSON Question

How to extract a specific data from the json using python

I want to extract the "long_name" data present in the "address_components" in the below given json. The challenge is I want only the "long_name" data whose "types" is "administrative_area_level_2". please let me know how to extract only that specific data preferably in python. Thanks

edit: Note that js["results"][0]["address_components"][2]["long_name"] is not how I want to extract its value since ["address_components"][2] keeps varying for the different locations given as input. my main criteria is to extract data of "long_name" whose "types" is always "administrative_area_level_2"

{
"status": "OK",
"results": [
{
"geometry": {
"location_type": "APPROXIMATE",
"bounds": {
"northeast": {
"lat": 12.9177876,
"lng": 80.24104
},
"southwest": {
"lat": 12.875989,
"lng": 80.20860669999999
}
},
"viewport": {
"northeast": {
"lat": 12.9177876,
"lng": 80.24104
},
"southwest": {
"lat": 12.875989,
"lng": 80.20860669999999
}
},
"location": {
"lat": 12.9009877,
"lng": 80.2279301
}
},
"formatted_address": "Sholinganallur, Chennai, Tamil Nadu, India",
"place_id": "ChIJGzh_3nlbUjoRGz_-itQtu_8",
"address_components": [
{
"long_name": "Sholinganallur",
"types": [
"sublocality_level_1",
"sublocality",
"political"
],
"short_name": "Sholinganallur"
},
{
"long_name": "Chennai",
"types": [
"locality",
"political"
],
"short_name": "Chennai"
},
{
"long_name": "Kanchipuram",
"types": [
"administrative_area_level_2",
"political"
],
"short_name": "Kanchipuram"
},
{
"long_name": "Tamil Nadu",
"types": [
"administrative_area_level_1",
"political"
],
"short_name": "TN"
},
{
"long_name": "India",
"types": [
"country",
"political"
],
"short_name": "IN"
}
],
"partial_match": true,
"types": [
"sublocality_level_1",
"sublocality",
"political"
]
}
]
}


The below is the code which I am trying but I get output only till location. Let me know what changes I need to make in my code.

import urllib
import json

apiurl= 'http://maps.googleapis.com/maps/api/geocode/json?'

while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break

url = apiurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url).read()

print 'Retrieved',len(uh),'characters'
js = json.loads(str(uh))
print json.dumps(js, indent=4)
jayz = js["results"][0]
lat = jayz["geometry"]["location"]["lat"]
lng = jayz["geometry"]["location"]["lng"]
print 'lat',lat,'lng',lng
location = jayz['formatted_address']
print location
components = js["results"][0]["address_components"]
names = [component['long_name'] for component in components if 'administrative_area_level_2' in component['types']]
name = names[0]
print "District:", name

Answer

You can get the components and loop on them and find the ones that you

js = eval(input_file.read())
components =  js["results"][0]["address_components"]
names = [component['long_name'] for component in components if 'administrative_area_level_2' in component['types']]

Here, names will include long_name of all the components that have administrative_area_level_2 in their types