# Getting JSON from the body of a POST

**URL:** <https://www.ruby-forum.com/t/getting-json-from-the-body-of-a-post/113570>\
**Category:** Rails\
**Created:** [September 2, 2007, 12:14am UTC](https://www.ruby-forum.com/t/getting-json-from-the-body-of-a-post/113570 "2007-09-02T00:14:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [September 2, 2007, 12:14am UTC](https://www.ruby-forum.com/t/getting-json-from-the-body-of-a-post/113570/1 "2007-09-02T00:14:35Z")

</div>

I have a Flash application that is sending JSON data to a Rails app. I  
want to be able to read the JSON from the controller. Does anyone know  
where I can go to figure this out? I’ve Google’d around but can only  
find instances of converting to JSON, not the other way.

Example:

Flash -\> Sends JSON -\> Rails Controller receives JSON and converts it to  
a class

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [September 2, 2007, 2:20am UTC](https://www.ruby-forum.com/t/getting-json-from-the-body-of-a-post/113570/2 "2007-09-02T02:20:34Z")

</div>

Mark H. wrote:

> Flash -\> Sends JSON -\> Rails Controller receives JSON and converts it to  
> a class

This is what I’ve come up with so far in my controller:

incoming\_data = request.env[‘RAW\_POST\_DATA’]  
my\_json\_stuff = incoming\_data.to\_json

I’m guessing there is a better way? Also, how do I access the JSON  
data? Ive tried:

my\_json\_stuff[‘myValue’]

and

my\_json\_stuff.myValue

etc.

Nothing seems to work.

---

<div class="post-metadata">

**Author:** ![markh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/7993a0/32.png) [@markh](https://www.ruby-forum.com/u/markh)\
**Post date:** [September 2, 2007, 1:26pm UTC](https://www.ruby-forum.com/t/getting-json-from-the-body-of-a-post/113570/3 "2007-09-02T13:26:06Z")

</div>

Mark H. wrote:

> This is what I’ve come up with so far in my controller:
> 
> incoming\_data = request.env[‘RAW\_POST\_DATA’]  
> my\_json\_stuff = incoming\_data.to\_json

I think you’ve got this the wrong way round?  
Flash should be sending the data as string encoded using JSON,  
you want to make it back into a ruby structure.

Using “to\_json” on a hash, will make it into a JSON string,  
you want to use “JSON.parse”

> > require ‘json’  
> > =\> []
> 
> > hash = {:a =\> {:b =\> :c}}  
> > =\> {:a=\>{:b=\>:c}}
> 
> > json\_1 = hash.to\_json  
> > =\> “{“a”:{“b”:“c”}}”
> 
> > json\_2 = JSON.unparse(hash)  
> > =\> “{“a”:{“b”:“c”}}”
> 
> > JSON.parse(json\_1)  
> > =\> {“a”=\>{“b”=\>“c”}}

Saying that, I don’t know how Flash would be sending you the data.  
I guess you need to do some debugging on params and RAW\_POST\_DATA until  
you find your JSON.
