Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to create a json-array with respect to my data. But in my data model, some of the fields are unnecessary and some of the fields name need to be changed for the json-array. Therefore I created a simple function to convert one object into a json format that I need to use :

  def testMethod
    {title: self.title, start: self.start_date, end: self.end_date, resource: Resource.find(self.resource_id).name}.to_json
  end

So the output for this json object is:

{:title=>"Test", :start=>Thu, 27 Dec 2012 10:25:00 UTC +00:00, :end=>Thu, 27 Dec 2012 10:25:00 UTC +00:00, :resource=>"Resource1"}

But when I iterate all of my objects with the following loop:

@calJson   = []
@calendars.each do |cal|
    @calJson.push(cal.testMethod)
end

I'll have this useless string:

["{\"title\":\"Test\",\"start\":\"2012-12-27T10:25:00Z\",\"end\":\"2012-12-27T10:25:00Z\",\"resource\":\"Resource1\"}", "{\"title\":\"ikincii\",\"start\":\"2012-12-27T10:25:00Z\",\"end\":\"2012-12-27T10:25:00Z\",\"resource\":\"Resource2\"}", "{\"title\":\"b da son\",\"start\":\"2012-12-27T10:27:00Z\",\"end\":\"2012-12-27T10:27:00Z\",\"resource\":\"Resource1\"}"]

Whereas I want something like this:

[ { title: 'Lunch 12.15-14.45', start: new Date(y, m, d, 12, 15), end: new Date(y, m, d, 14, 45),

              resource: 'resource1'
          },              
          {
              title: 'Meeting',
              start: new Date(y, m, d, 10, 30),
              end: new Date(y, m, d+4, 11, 00),

              resource: 'resource1'
          } ]

How should I do that ?

Thanks.

share|improve this question
1  
Just don't call to_json. It turns your hash into a JSON string. And you want to work with a hash. –  Sergio Tulentsev Dec 27 '12 at 13:11

2 Answers 2

up vote 1 down vote accepted

To answer your question straight:

 def testMethod
  {title: self.title, start: self.start_date, end: self.end_date, resource: Resource.find(self.resource_id).name}
 end

 @calJson = @calendars.each_with_object([]) {|cal, array| array << cal.testMethod }.to_json

But you have better alternatives: json builders are dedicated to this kind of tasks.

share|improve this answer
    
Thank you very much. One more question: Is there any way that I can return start: new Date(y, m, d, 10, 30) in my JSON ? I mean without quotation marks. Because javascript doesn't understand this –  CanCeylan Dec 27 '12 at 13:48
    
you should pass the date as a string and convert if client side. Example, if you pass the basic result of a created_at: "2011-02-15 12:25:33 +0100", then you can do: var date = new Date("2011-02-15 12:25:33 +0100") –  apneadiving Dec 27 '12 at 13:56
    
Another alternative is to make a custom date format object. I had this problem writing json for google charts, so I created a custom GoogleDate object that had its own to_json method. –  John Jul 16 '14 at 8:16
    
@John agreed, its much nicer to create dedicated objects –  apneadiving Jul 16 '14 at 8:41
    
@apneadiving you also don't have the problems from monkey patching the date,datetime,activesupport::timewithzone,time objects and overriding their to_json methods. Its also nice because you can put these inside of an array or hash and they will produce the correct clean code. –  John Jul 16 '14 at 16:06

Try adding this to your corresponding controller:

respond_to do |format|
  format.html
  format.js { render json: @events }
end

Also, in my Event model, I'm overriding default as_json method with this one:

def as_json(options = {})
{
  :id => self.id,
  :title => self.name,
  :description => self.description || "",
  :start => starts_at.rfc822,
  :end => ends_at.rfc822,
  :allDay => self.all_day
}
end
share|improve this answer
    
But the problem is there are some fields that I don't want to have. And some fields that I want their name to be changed –  CanCeylan Dec 27 '12 at 13:11
    
In that case, I guess overriding as_json could help you. Thanks for the downvote for trying to help. Do you expect people to help you if you downvote every answer that didn't solve your problem, but the user tried to help you? –  Viktor Fonic Dec 27 '12 at 13:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.