# Having troubles extracting json data within javascript

**URL:** <https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957>\
**Category:** Rails\
**Created:** [July 24, 2008, 5:30am UTC](https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957 "2008-07-24T05:30:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 24, 2008, 5:30am UTC](https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957/1 "2008-07-24T05: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  
console.log(data)  
console.log(data[“latitude”])

Results in:  
{“latitude”: “55.272766”, “longitude”: “-114.762116”}  
undefined

Checking a javascript created hash  
var foo = {“foo”: “footoyou”, “bar”: “barandbeyond”}  
console.log(foo)  
console.log(foo[“foo”])

Shows me  
Object foo=footoyou bar=barandbeyond  
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  
respond\_to do |format|  
format.js { render :json =\> coords.to\_json, :layout =\> false}  
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:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 24, 2008, 5:44am UTC](https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957/2 "2008-07-24T05:44:26Z")

</div>

Just after I post I find that the following works  
console.log(eval(’(’ + data + ‘)’))

Is there a cleaner way of doing this?

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 24, 2008, 6:47pm UTC](https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957/3 "2008-07-24T18:47:58Z")

</div>

On Wed, Jul 23, 2008 at 10:44 PM, Chris O.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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)

---

<div class="post-metadata">

**Author:** ![chrisolsen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c6cbf5/32.png) [@chrisolsen](https://www.ruby-forum.com/u/chrisolsen)\
**Post date:** [July 24, 2008, 7:05pm UTC](https://www.ruby-forum.com/t/having-troubles-extracting-json-data-within-javascript/143957/4 "2008-07-24T19:05:18Z")

</div>

Christopher Kintner wrote:

> On Wed, Jul 23, 2008 at 10:44 PM, Chris O.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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)

I am using jQuery. On the [json.org](http://json.org) page there is some javascript  
available that will do the trick. I figured that since json is such a  
huge part of javascript that the functionality I was looking for was  
already there, just hiding.

Thanks for the help.
