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 8886521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:34:43+00:00 2026-06-14T21:34:43+00:00

I am trying to pass a hash into Javascript via JSON where the keys

  • 0

I am trying to pass a hash into Javascript via JSON where the keys are ruby objects, and the values are arrays of objects. The arrays of objects are transmitted just fine, but the key is being converted into a string of the class.

Here is an example:

[4] pry(#<User>)> x = find_all_sections.collect { |s| { s => s.find_all_events_id_and_title } }
=> [{#<Section id: 58, course_id: 12, section_number: 3, semester_id: 1>} =>
   [{:id=>37, :title=>"Event 37"},
    {:id=>40, :title=>"Event 40"},
    {:id=>9, :title=>"Event 9"},
    {:id=>10, :title=>"Event 10"},
    {:id=>16, :title=>"Event 16"},
    {:id=>38, :title=>"Event 38"}, etc...

The result of converting this to json is (you just have to look at the first few characters of the string to see that it’s not the object, but the to_s of the object:

[11] pry(#<User>)> x.to_json
=> "[{\"#<Section:0x007fa475b52f68>\":[{\"id\":37,\"title\":\"Event 37\"},{\"id\":40,\"title\":\"Event 40\"},{\"id\":9,\"title\":\"Event 9\"},{\"id\":10,\"title\":\"Event 10\"},{\"id\":16,\"title\":\"Event 16\"},{\"id\":38,\"title\":\"Event 38\"},{\"id\":49,\"title\":\"Event 49\"},{\"id\":39,\"title\":\"Event 39\"},{\"id\":15,\"title\":\"Event 15\"},{\"id\":25,\"title\":\"Event 25\"},{\"id\":11,\"title\":\"Event 11\"},{\"id\":4,\"title\":\"Event 4\"},{\"id\":22,\"title\":\"Event 22\"},{\"id\":1,\"title\":\"Event 1\"},{\"id\":23,\"title\":\"Event 23\"},{\"id\":8,\"title\":\"Event 8\"},{\"id\":13,\"title\":\"Event 13\"},{\"id\":26,\"title\":\"Event 26\"},{\"id\":46,\"title\":\"Event 46\"},{\"id\":20,\"title\":\"Event 20\"},{\"id\":31,\"title\":\"Event 31\"},{\"id\":6,\"title\":\"Event 6\"},{\"id\":18,\"title\":\"Event 18\"},{\"id\":41,\"title\":\"Event 41\"},{\"id\":7,\"title\":\"Event 7\"},{\"id\":43,\"title\":\"Event 43\"},{\"id\":45,\"title\":\"Event 45\"},{\"id\":24,\"title\":\"Event 24\"},{\"id\":2,\"title\":\"Event 2\"},{\"id\":44,\"title\":\"Event 44\"},{\"id\":29,\"title\":\"Event 29\"},{\"id\":28,\"title\":\"Event 28\"},{\"id\":5,\"title\":\"Event 5\"},{\"id\":3,\"title\":\"Event 3\"},{\"id\":27,\"title\":\"Event 27\"}]},

How can I achieve the JSON data keeping the key’s object intact?

  • 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-14T21:34:44+00:00Added an answer on June 14, 2026 at 9:34 pm

    You can’t. Keys are always strings in JavaScript. Furthermore, the JSON spec says that keys must always have the form of a single, double-quoted string literal.

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

Sidebar

Related Questions

I am trying to pass values from a ruby hash to a bash script..
I am trying to push keys and values into a hash as I iterate
I am trying to pass the fullcalendar events to my controller via Json, however
I'm trying to pass data between blocks using sinatra. For example: @data = Hash.new
I am trying to generate a SHA256 hash in android, that I then pass
I am trying to pass a has such as http://mysite.com/#32132 via JS to a
I am trying to convert a test password (testing4) into a phpbb3 hash. This
I am basically trying to pass a string and a hash to a subroutine
I am trying to pass ConnectODBC(%settings , @connectionString); hash abd then array In the
I am trying pass class as value in hashmap. I need to get the

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.