0

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?

2 Answers 2

3

Put values in list and then join it using ","

elements = []

coordinates = data["results"][0]["geometry"]["location"]

for i in data["results"][0]["address_components"]:
   elements.append( i["long_name"] )

elements.append( str(coordinates['lat']) ) # convert to string to use `join`
elements.append( str(coordinates['lng']) ) # convert to string to use `join`

print ", ".join(elements)

-

277, Bedford Avenue, Williamsburg, Brooklyn, Kings County, New York, United States, 11211, 40.714232, -73.9612889
Sign up to request clarification or add additional context in comments.

Comments

-1

I don't understand what is your problem:

for result in data["results"]:
    address = address = {place['types'][0]: place ['long_name'] for place in data["address_components"]}
    address['lat'] = float(result["geometry"]["location"]['lat'])
    address['lng'] = float(result["geometry"]["location"]['lng'])
    print address

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.