# Having troubles extracting json data within javascript

**URL:** <https://discuss.rubyonrails.org/t/having-troubles-extracting-json-data-within-javascript/32299>\
**Category:** rubyonrails-talk\
**Created:** [July 24, 2008, 3:30am UTC](https://discuss.rubyonrails.org/t/having-troubles-extracting-json-data-within-javascript/32299 "2008-07-24T03:30:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 24, 2008, 3:30am UTC](https://discuss.rubyonrails.org/t/having-troubles-extracting-json-data-within-javascript/32299/1 "2008-07-24T03:30:50Z")

</div>

I am having some difficulty extracting json data from a returned hash table.

From the geocode data that is returned the following &nbsp;&nbsp;console.log(data) &nbsp;&nbsp;console.log(data["latitude"])

Results in: &nbsp;&nbsp;{"latitude": "55.272766", "longitude": "-114.762116"} &nbsp;&nbsp;undefined

Checking a javascript created hash &nbsp;&nbsp;var foo = {"foo": "footoyou", "bar": "barandbeyond"} &nbsp;&nbsp;console.log(foo) &nbsp;&nbsp;console.log(foo["foo"])

Shows me &nbsp;&nbsp;Object foo=footoyou bar=barandbeyond &nbsp;&nbsp;footoyou

So obviously the json data is not is a json form, but rather just text.

Here is my controller code, where coords is the hash table &nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;format.js { render :json =\> coords.to\_json, :layout =\> false} &nbsp;&nbsp;end

I am getting the exact same output if I use :json =\> coords.to\_json or just :json =\> coords

Help is appreciated.

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [July 24, 2008, 3:44am UTC](https://discuss.rubyonrails.org/t/having-troubles-extracting-json-data-within-javascript/32299/2 "2008-07-24T03:44:26Z")

</div>

Just after I post I find that the following works &nbsp;&nbsp;console.log(eval('(' + data + ')'))

Is there a cleaner way of doing this?

---

<div class="post-metadata">

**Author:** ![Christopher\_Kintner](https://avatars.discourse-cdn.com/v4/letter/c/59ef9b/32.png) [@Christopher\_Kintner](https://discuss.rubyonrails.org/u/Christopher_Kintner)\
**Post date:** [July 24, 2008, 2:19pm UTC](https://discuss.rubyonrails.org/t/having-troubles-extracting-json-data-within-javascript/32299/3 "2008-07-24T14:19:09Z")

</div>

> Just after I post I find that the following works console.log(eval('(' + data + ')'))
> 
> Is there a cleaner way of doing this?

if you are using prototype you could use data.evalJSON(true)

See: [Prototype API Documentation | String#evalJSON (Deprecated URL)](http://www.prototypejs.org/api/string/evalJSON)
