I'm new to python and trying to parse a JSON from a Google Geocoding response with Python 2.7. The entire JSON file can be found here: https://dl.dropboxusercontent.com/u/60455118/data.json
Sample JSON:
{
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kings County",
"short_name" : "Kings County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Ave, Brooklyn, NY 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
I'd like the output to be in this format:
227, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, Latitude, Longitude
My script is as follows:
import json
from pprint import pprint
with open('c:\scripts\data.json') as f:
data = json.load(f)
coordinates = data["results"][0]["geometry"]["location"]
for i in data["results"][0]["address_components"]:
print i["long_name"]
It outputs in this format:
277
Bedford Avenu
Williamsburg
Brooklyn
Kings County
New York
United States
11211
How do I transpose this and add in the Lat Lon values?