Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9087861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:47:41+00:00 2026-06-16T21:47:41+00:00

I need to create a json-array with respect to my data. But in my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:47:42+00:00Added an answer on June 16, 2026 at 9:47 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data and need to create a json file with this structure
Possible Duplicate: PHP : Create array for JSON I need to output data in
I need to create a json message like this: { success:true, count:3, data: [
see following code. I need to serialize a list of data into JSON array:
i m new in android .and i need to create filter for json array
I Create the JSON Array like in the name of jarray . It has
I need to create a map/array for auto complete from a JSON response and
I need to create data for a JSON request with the inputs taken from
I need to create an array which will give me the following json encoded
I need to create a custom json for the jit library. Should I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.