# Newbie Q - JSON

**URL:** <https://www.ruby-forum.com/t/newbie-q-json/189128>\
**Category:** Rails\
**Created:** [May 25, 2010, 4:02am UTC](https://www.ruby-forum.com/t/newbie-q-json/189128 "2010-05-25T04:02:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 25, 2010, 4:02am UTC](https://www.ruby-forum.com/t/newbie-q-json/189128/1 "2010-05-25T04:02:04Z")

</div>

The recently released Google maps API v3 returns geocoding info in json  
format as shown below. I am sure there is an elegant way to extract the  
lat/lng but I have failed to find it after many attempts - can anyone  
help? Thanks in advance.

{  
“status”: “OK”,  
“results”: [ {  
“types”: [“point\_of\_interest”, “establishment”],  
“formatted\_address”: “Buckingham Palace, Westminster, London, UK”,  
“address\_components”: [ {  
“long\_name”: “Buckingham Palace”,  
“short\_name”: “Buckingham Palace”,  
“types”: [“point\_of\_interest”, “establishment”]  
}, {  
“long\_name”: “London”,  
“short\_name”: “London”,  
“types”: [“locality”, “political”]  
}, {  
“long\_name”: “Westminster”,  
“short\_name”: “Westminster”,  
“types”: [“administrative\_area\_level\_3”, “political”]  
}, {  
“long\_name”: “Greater London”,  
“short\_name”: “Gt Lon”,  
“types”: [“administrative\_area\_level\_2”, “political”]  
}, {  
“long\_name”: “England”,  
“short\_name”: “England”,  
“types”: [“administrative\_area\_level\_1”, “political”]  
}, {  
“long\_name”: “United Kingdom”,  
“short\_name”: “GB”,  
“types”: [“country”, “political”]  
} ],  
“geometry”: {  
“location”: {  
“lat”: 51.5007890,  
“lng”: -0.1422640  
},  
“location\_type”: “APPROXIMATE”,  
“viewport”: {  
“southwest”: {  
“lat”: 51.4938426,  
“lng”: -0.1582714  
},  
“northeast”: {  
“lat”: 51.5077344,  
“lng”: -0.1262566  
}  
}  
}  
} ]  
}

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 25, 2010, 4:33am UTC](https://www.ruby-forum.com/t/newbie-q-json/189128/2 "2010-05-25T04:33:27Z")

</div>

Hi Henry,

Doesn’t include any err testing/checking, but … you could do  
something like:

### in some rails project root:

$ cat ./tmp/t.runnable  
def parse\_lat\_lng(json\_str)  
lat, lng = [0.0, 0.0]  
location = JSON.parse(json\_str)[‘results’][0][‘geometry’]  
[‘location’]  
lat, lng = location[‘lat’], location[‘lng’]  
return [lat, lng]  
end

#------------------  
json\_str = \<\<EOS  
{  
“status”: “OK”,  
“results”: [ {  
“types”: [“point\_of\_interest”, “establishment”],  
“formatted\_address”: “Buckingham Palace, Westminster, London, UK”,  
“address\_components”: [ {  
“long\_name”: “Buckingham Palace”,  
“short\_name”: “Buckingham Palace”,  
“types”: [“point\_of\_interest”, “establishment”]  
}, {  
“long\_name”: “London”,  
“short\_name”: “London”,  
“types”: [“locality”, “political”]  
}, {  
“long\_name”: “Westminster”,  
“short\_name”: “Westminster”,  
“types”: [“administrative\_area\_level\_3”, “political”]  
}, {  
“long\_name”: “Greater London”,  
“short\_name”: “Gt Lon”,  
“types”: [“administrative\_area\_level\_2”, “political”]  
}, {  
“long\_name”: “England”,  
“short\_name”: “England”,  
“types”: [“administrative\_area\_level\_1”, “political”]  
}, {  
“long\_name”: “United Kingdom”,  
“short\_name”: “GB”,  
“types”: [“country”, “political”]  
} ],  
“geometry”: {  
“location”: {  
“lat”: 51.5007890,  
“lng”: -0.1422640  
},  
“location\_type”: “APPROXIMATE”,  
“viewport”: {  
“southwest”: {  
“lat”: 51.4938426,  
“lng”: -0.1582714  
},  
“northeast”: {  
“lat”: 51.5077344,  
“lng”: -0.1262566  
}  
}  
}  
} ]  
}  
EOS

lat, lng = parse\_lat\_lng(json\_str)  
puts “lat=#{lat} lng=#{lng}”

$ ./script/runner ./tmp/t.runnable  
lat=51.500789 lng=-0.142264

Jeff

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [May 25, 2010, 10:31am UTC](https://www.ruby-forum.com/t/newbie-q-json/189128/3 "2010-05-25T10:31:35Z")

</div>

Jeff B.systems wrote:

> Hi Henry,  
>   
> Jeff

Jeff,

Thanks very much. I had restricted all my searching to from\_json.

Henry
