Trying to extract specific info from Google geocode JSON

537 views
Skip to first unread message

Greg

unread,
Oct 24, 2017, 1:28:32 AM10/24/17
to OpenRefine
Hello.

I was able to successfully geocode a big batch of addresses -- and extract the lat and long from the JSON -- thanks to the helpful explainer on Github. But I'm stuck on trying to extract info from "address_components" in the Google geocoder JSON. Specially, I'm trying to extract the info under types [ "locality", "political" ] . 

I've tried using value.parseJson().results[0].address_components[3].long_name -- and that sort of works. The problem is the info under that location in the array varies among the entries, so I end up pulling different categories from entry to entry.

Any help is greatly appreciated.

Ettore Rizza

unread,
Oct 24, 2017, 2:41:40 AM10/24/17
to OpenRefine
Hi Greg, 

here is an example of Json from Google geocoder : https://maps.googleapis.com/maps/api/geocode/json?address=16,%20rue%20du%20moulin,%20Chapelle-lez-herlaimont,%20Belgique

The items you want are not easy to parse with GREL. Depending on the address, the element "types" can refer to the route, the locality, the administrative_area_level_2 ... I suggest you use Python/Jython instead. Assuming you want to retrieve the long name of the locality, here is what the code might look like. Feel free to adapt it to your real needs.

import json

data
= json.loads(value)

for el in data["results"]:
   
for i, adress in enumerate(el["address_components"]):
       
if "locality" in adress['types']:
           
return el["address_components"][i]["long_name"]



Greg

unread,
Oct 24, 2017, 12:46:32 PM10/24/17
to OpenRefine
Thank you! I was able to successfully use that code to extract the locality and then adjust it to extract a few other items. 

I had hit a wall on this and you helped me over it. (And now I'm thinking I need to start learning about python.) 

It's much appreciated!

Ettore Rizza

unread,
Oct 24, 2017, 2:50:25 PM10/24/17
to OpenRefine
You're welcome. No need to know Python in all its details to use some scripts in Open Refine. You will find on the net thousands of examples that will be enough to customize a little.
Reply all
Reply to author
Forward
0 new messages