Getting JSON from the body of a POST

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

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.

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.