# How to handle json data request?

**URL:** <https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531>\
**Category:** Rails\
**Created:** [April 2, 2009, 9:41am UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531 "2009-04-02T09:41:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [April 2, 2009, 9:41am UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531/1 "2009-04-02T09:41:01Z")

</div>

I have searched in this group for a while, and it seems no one face  
the same problem like me yet. I am developing a rest web service(post  
method) that requires json data format from my javascript client  
application. I don’t know how to handle since I could not get data  
that is being sent. In my controller, params variable got messed with  
key and value. I want to know the way how to parse data from this to  
ruby object. Any idea is really appreciated.

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [April 2, 2009, 10:19am UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531/2 "2009-04-02T10:19:11Z")

</div>

On Apr 2, 2009, at 3:40 AM, Chamnap wrote:

> I have searched in this group for a while, and it seems no one face  
> the same problem like me yet. I am developing a rest web service(post  
> method) that requires json data format from my javascript client  
> application. I don’t know how to handle since I could not get data  
> that is being sent. In my controller, params variable got messed with  
> key and value. I want to know the way how to parse data from this to  
> ruby object. Any idea is really appreciated.

Get the JSON gem for starters.

sudo gem install json

Then in your code:  
require ‘rubygems’  
gem ‘json’  
require ‘json’

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [April 3, 2009, 3:28am UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531/3 "2009-04-03T03:28:12Z")

</div>

On Apr 2, 3:18 pm, Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Get the JSON gem for starters.
> 
> sudo gem install json
> 
> Then in your code:  
> require ‘rubygems’  
> gem ‘json’  
> require ‘json’

I have followed what you said, but still doesn’t work. The params  
variable still contains weird string like: {“{"first\_name":"chamnap  
","last\_name":"chhorn"}”=\>nil, “action”=\>“create”,  
“controller”=\>“users”}. Therefore, I could not access through either  
params[:first\_name] or params[:last\_name].

Chamnap

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [April 5, 2009, 5:54am UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531/4 "2009-04-05T05:54:43Z")

</div>

Thanks

I also have founded a method in ActiveSupport::JSON.decode, and it  
does work the same thing as you did show me. What do you think?

Chamnap

---

<div class="post-metadata">

**Author:** ![chamnap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/779978/32.png) [@chamnap](https://www.ruby-forum.com/u/chamnap)\
**Post date:** [April 3, 2009, 2:39pm UTC](https://www.ruby-forum.com/t/how-to-handle-json-data-request/164531/5 "2009-04-03T14:39:08Z")

</div>

On Apr 2, 2009, at 9:27 PM, Chamnap wrote:

> > require ‘rubygems’  
> > gem ‘json’  
> > require ‘json’
> 
> I have followed what you said, but still doesn’t work. The params  
> variable still contains weird string like: {“{"first\_name":"chamnap  
> ","last\_name":"chhorn"}”=\>nil, “action”=\>“create”,  
> “controller”=\>“users”}. Therefore, I could not access through either  
> params[:first\_name] or params[:last\_name].
> 
> Chamnap

irb\> require ‘rubygems’  
=\> true  
irb\> gem ‘json’  
=\> true  
irb\> require ‘json’  
=\> true  
irb\> raw = “{"first\_name":"chamnap","last\_name":"chhorn"}”  
=\> “{"first\_name":"chamnap","last\_name":"chhorn"}”  
irb\> puts raw  
{“first\_name”:“chamnap”,“last\_name”:“chhorn”}  
=\> nil  
irb\> JSON(raw)  
=\> {“first\_name”=\>“chamnap”, “last\_name”=\>“chhorn”}  
irb\> cooked = JSON.parse(raw)  
=\> {“first\_name”=\>“chamnap”, “last\_name”=\>“chhorn”}  
irb\> raw  
=\> “{"first\_name":"chamnap","last\_name":"chhorn"}”  
irb\> cooked  
=\> {“first\_name”=\>“chamnap”, “last\_name”=\>“chhorn”}  
irb\> raw.class  
=\> String  
irb\> cooked.class  
=\> Hash  
irb\> cooked[“first\_name”]  
=\> “chamnap”  
irb\> cooked[:first\_name]  
=\> nil

Note that JSON.parse is going to return a normal Ruby Hash, not a  
Rails ActiveSupport HashWithIndifferentAccess. You’ll have to use  
“first\_name” as the key, not :first\_name. Show the code that you’re  
trying to use that’s pulling that whole string into a key of the  
params hash.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
