# Rails, REST and JSON

**URL:** <https://www.ruby-forum.com/t/rails-rest-and-json/90454>\
**Category:** Rails\
**Created:** [March 28, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454 "2007-03-28T23:00:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 28, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/1 "2007-03-28T23:00:36Z")

</div>

Hi everyone,

I’m writing a lightweight AJAX application using Rails on the server  
side as  
a RESTful web service provider.  
I need the web service to support both JSON and XML I/O. Outputting data  
in  
XML and JSON is easy (using to\_xml and to\_json),  
and it’s also easy to do XML input as Rails does it for you  
automatically.  
Is it possible to somehow have the same  
automatic parsing with JSON?

Erik

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 28, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/2 "2007-03-28T23:09:38Z")

</div>

On 3/28/07, Erik A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Erik
> 
> >

Hey Erik,

afaik, no. I only have to output stuff to json one place in my app,  
so I just wrote a basic to\_json method:

def to\_json  
video = attributes.reject { |k, v| ignored\_attributes.include?(k) }  
video[“video\_id”] = video\_id  
video[“tags”] = tags.collect { |t| t.name }  
video[“created\_at”] = created\_at.to\_s  
video[“assets”] = assets  
video[“screenshots”] = screenshots  
video[“site-name”] = site\_name  
video.to\_json  
end

That’s taken us as far as we need to so far.

Pat

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 29, 2007, 12:56am UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/3 "2007-03-29T00:56:48Z")

</div>

On 3/28/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey pat,
> 
> My question was about JSON input and parsing, not output.
> 
> Erik

Oops, I’m sorry.

Well the first thing you should know is that json is super close to  
yaml. The only difference afaik is that yaml cares about whitespace,  
json doesn’t.

So basically what you’d want to do is read the json, normalize it so  
that the whitespace is good, and then treat it as yaml.

I also think I remember reading something from \_why a while back, you  
might try checking out his site.

Pat

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 29, 2007, 12:21am UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/4 "2007-03-29T00:21:45Z")

</div>

Hey pat,

My question was about JSON input and parsing, not output.

Erik

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 29, 2007, 1:25am UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/5 "2007-03-29T01:25:51Z")

</div>

Doesn’t look like it… If I grep from\_xml in my gems dir, I find a  
bunch of results, including the modification to Hash that I’m pretty  
sure rails uses to convert incoming xml into a params hash. But grepping  
for “from\_json” doesn’t return anything. Not very scientific, I know,  
but…

Unfortunately you’ll probably have to modify rails to allow submitting  
json. It would probably be less work to write the javascript to build  
the xml… or better yet, just create the param names in the nested-hash  
notation that rails would expect (like “account[name]=foo”).

Or maybe some rails guru out there knows something I don’t…

b

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 30, 2007, 12:13am UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/6 "2007-03-30T00:13:23Z")

</div>

If you are building a RESTful application why are you pushing JSON to  
the server? If this is truly RESTful then you should be simply  
posting/putting data to your already available REST controller  
actions.

Same thing with XML. This is not SOAP. You don’t have to send XML in  
both directions. Just use REST like it is intended to be used and  
you’re good to go.

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 30, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/7 "2007-03-30T15:27:33Z")

</div>

I am in the process of writing a REST based interface between my RoR  
application and a Cocoa desktop application. While I receive data in  
XML I send data directly to the Rails application using a simple POST/  
PUT with data encoded into the HTTP message body as standard form  
data.

Sorry if my previous post was unclear. I just don’t see the need to  
encode the data as XML or JSON for sending. But maybe this is  
completely different for AJAX, but I don’t see why it would be. I’ve  
not done much AJAX yet, but hope to in the future.

---

<div class="post-metadata">

**Author:** ![seeah](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b19c9b/32.png) [@seeah](https://www.ruby-forum.com/u/seeah)\
**Post date:** [March 30, 2007, 1:05am UTC](https://www.ruby-forum.com/t/rails-rest-and-json/90454/8 "2007-03-30T01:05:26Z")

</div>

On Mar 29, 2007, at 6:12 PM, Robert W. wrote:

> If you are building a RESTful application why are you pushing JSON  
> to the server? If this is truly RESTful then you should be simply  
> posting/putting data to your already available REST controller  
> actions.
> 
> Same thing with XML. This is not SOAP. You don’t have to send XML  
> in both directions. Just use REST like it is intended to be used  
> and you’re good to go.

I’m having trouble following this response. If you’re building a  
RESTful application you have to be able to send and receive  
representations of your response, whether that’s in XML, JSON, HTML,  
or any other format. It seems like the original poster was just  
asking how best to understand input sent as JSON?

It’s actually quite easy to get rails to accept JSON as an input  
format, provided the data structures in the JSON match what the rails  
app expects. I wrote a few blog entries which cover that:

> **[Input formats and content types in Rails 1.2](https://jystewart.net/2007/02/03/input-formats-and-content-types-in-rails-12/)**
>
> One feature of recent releases of Rails I hadn’t spotted before is the ability to define your own parameter parsing based on content type. I’m working on an application that will employ…

in-rails-12/

> **[Improvements to Rails’ JSON Support](https://jystewart.net/2007/03/18/improvements-to-rails-json-support/)**
>
> I’m travelling at the moment, so posting has slowed down after three weeks of daily updates. Nevertheless I’ve just about managed to keep up with my newsreader and was pleased to see th…

and a piece over at InfoQ that talks about how to build RESTful apps  
that speak XML, JSON and microformatted-HTML:

> **[Versatile RESTful APIs Beyond XML](https://www.infoq.com/articles/rails-rest-and-microformats/)**
>
> The world of RESTful resources that Rails firmly entered with version 1.2 naturally uses XML as its lingua franca. But there's no reason that it can't be multi-lingual, and thanks to the versatility of rails it's easy to support other standards...

James.

–  
James S.  
Work: [http://jystewart.net/process/](http://jystewart.net/process/)
