Welcome to Working With Rails


Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Practical Applications of JSON
5 Posts
Practical Applications of JSON


  1. Can you tell me where JSON is practically used in real world rails applications?

  2. I think usually, you convert Model to json format. And then what do you do with that json?

  3. How does one decide whether JSON is required in a particular rails application or not?


Let me answer (3) first, because it's the most important answer. Never, ever try to pre-empt requirements. If you're pondering whether or not JSON output is required - then it's not! Only add it in when a requirement arises that requires it.

So, (1) and (2) are essentially the same question.

JSON output is used mainly for AJAX requests by rich client-side (ie. javascript) components of your application. It makes the javascript code simpler because it doesn't have to do any parsing of the data like it would if it was receiving XML or some other format instead.

Another very common use of JSON is to wrap it in a callback function which lets client-side applications from other domains utilize the data from your site in a javascript component on their website without tripping over the usual cross-domain issues with AJAX.

So, as you can see these are very specific purposes - unless you need these specific features, then there's no reason to even consider it.

ok, so we could use it for AJAX requests. Few more questions are bothering me, 4. Is there any application where the data itself will be in JSON format (like sometimes the data is stored in XML)? Does this happen in real world apps? 5. What about YAML, what kind of data is stored in yml files? (other than fixtures). 6. How often or rarely is the data stored in yml / JSON? (instead of database) Thanks

People tend to use YAML for seed data and/or configuration data (because it's easier to change in a text editor than having to connect to a database and change it there). I don't know that I've heard of any reason to store data in JSON.

How often? I rarely do it, I don't really see the point of storing data in YAML when it's so simple to just store it in the DB, and provide a CRUD admin to the data for your users. Other people may have more obvious situations where YAML makes sense.

Thank you. You have answered my questions.

5 Posts
Login to add your message