Take the 2-minute tour ×

I am using rails-3

My table is countries and the fields are:

Id + name + created_at + updated_at

In my controller my query is

@country = Country.all

I want to convert my data into json. How can I do this ?

share|improve this question
if this answer helped you, feel free to check this answer as correct :) –  RGB Apr 23 '12 at 18:32
add comment

3 Answers

up vote 3 down vote accepted


then when youre at the clientside, you can capture a request us ajax with JSON.parse(result)

share|improve this answer
@manish-nautiyal if this answer helped you, feel free to check this answer as correct :) –  RGB Apr 23 '12 at 14:29
thx RGB it helps me. –  manish nautiyal Apr 24 '12 at 7:17
hit the check box and accept it as the correct answer! cdn.sstatic.net/img/faq/faq-accept-answer.png –  RGB Apr 24 '12 at 15:35
add comment

JSON (see http://www.json.org) is a notation methodology for representing objects. So you need to consider whether you want to think of the object as your table, or a record in the table. Your table may be too big to represent as an object. Most people represent records, or small record collections as objects in JSON.

Here's a good discussion of it: http://mike.bailey.net.au/2011/02/json-with-ruby-and-rails/

share|improve this answer
add comment

Suppose you have :-

@country = Country.all

and you to convert @country data into json.

 json_converted_data = JSON.parse(@country)

I guess this will work

share|improve this answer
this will not work...JSON.parse is a javascript call from the clientside...where as /@country is in a rails collection on the server side...it would work if /@country was a js variable containing the content of a json response –  RGB Apr 23 '12 at 14:18
Thanks RGB.. You are correct –  Apurva Mayank Apr 23 '12 at 14:36
add comment

Your Answer


