# JSON data not decoded in Rails

**URL:** <https://www.ruby-forum.com/t/json-data-not-decoded-in-rails/171568>\
**Category:** Rails\
**Created:** [July 5, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/json-data-not-decoded-in-rails/171568 "2009-07-05T21:06:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 5, 2009, 9:06pm UTC](https://www.ruby-forum.com/t/json-data-not-decoded-in-rails/171568/1 "2009-07-05T21:06:42Z")

</div>

Hello all,

I’m trying to send some json data from the browser to the server using  
prototype without success. On the server side ActiveSupport::JSON.decode  
isn’t decoding the data.

Here is the code:

Javacript:  
click

function send() { var send\_data\_url = "/pages/change"; var json\_parameters = $H({name: 'Violet', occupation: 'character', age: 25 }).toJSON(); new Ajax.Request(send\_data\_url, { method: 'post', parameters: json\_parameters, contentType: 'application/json;' }); } Rails: def change() debugger my\_json = ActiveSupport::JSON.decode(params["\_json"]) render :text =\> my\_json end I've checked with the debugger that the json data that is sent to the server is the following: {"\_json"=\>"%7B%22name%22%3A%20%22Violet%22%2C%20%22occupation%22%3A%20%22charact er%22%2C%20%22age%22%3A%2025%7D", "action"=\>"change", "controller"=\>"pages"} Can anyone help me? Thanks. Best regards, Migrate

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 5, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/json-data-not-decoded-in-rails/171568/2 "2009-07-05T22:42:31Z")

</div>

On Jul 5, 3:06 pm, Hu Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello all,
> 
> I’m trying to send some json data from the browser to the server using  
> prototype without success. On the server side ActiveSupport::JSON.decode  
> isn’t decoding the data.

What happens ? render :text =\> my\_json might produce some odd output  
since you’re just calling to\_s on a hash which doesn’t do anything  
very interesting. render :text =\> my\_json.inspect() would produce more  
readable output

Fred

---

<div class="post-metadata">

**Author:** ![migrate](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/bc79bd/32.png) [@migrate](https://www.ruby-forum.com/u/migrate)\
**Post date:** [July 5, 2009, 10:58pm UTC](https://www.ruby-forum.com/t/json-data-not-decoded-in-rails/171568/3 "2009-07-05T22:58:45Z")

</div>

Hello Fred,

Thanks for the answer but the problem remains.

The “render :text =\> my\_json” part is just a debug line I added.  
I’m interested in manipulating the json data before sending an answer to  
the browser.  
I’ve done what you said but my\_json.inspect() returns the same output.

From what I gathered the my\_json is returned as a String object instead  
of an Hash.

Example:  
(rdb:5) my\_json = ActiveSupport::JSON.decode(params[“\_json”])  
“%7B%22name%22%3A%20%22Violet%22%2C%20%22occupation%22%3A%20%22character%22%2C%2  
0%22age%22%3A%2025%7D”  
(rdb:5) my\_json.class  
String

If I use the same json string directly in the controller in Rails the  
decode works so it must have something to do with the way I’m creating  
the Ajax request, but I still don’t know what I’m doing wrong.

Example:  
(rdb:5) my\_json2 = ActiveSupport::JSON.decode(“{name: ‘Violet’,  
occupation: ‘character’, age: 25 }”)  
{“occupation”=\>“character”, “name”=\>“Violet”, “age”=\>25}  
(rdb:5) my\_json2.class  
Hash

Best regards,  
Migrate

Frederick C. wrote:

> On Jul 5, 3:06ï¿½pm, Hu Ma [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hello all,
> > 
> > I’m trying to send some json data from the browser to the server using  
> > prototype without success. On the server side ActiveSupport::JSON.decode  
> > isn’t decoding the data.
> 
> What happens ? render :text =\> my\_json might produce some odd output  
> since you’re just calling to\_s on a hash which doesn’t do anything  
> very interesting. render :text =\> my\_json.inspect() would produce more  
> readable output
> 
> Fred
