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

  • SEARCH
  • Home
  • 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 8412537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:41:42+00:00 2026-06-10T00:41:42+00:00

I am trying to send slightly complicated from my Rails app through JSON to

  • 0

I am trying to send slightly complicated from my Rails app through JSON to a JS script to render the page in some interesting manner. Let us say the data in Rails is about people and their assets and looks like this:

[
  {:name => 'Bill Smith', :state => 'KS'} => [
    {:type => 'house', :valuation => 150000},
    {:type => 'car', :valuation => 10000}
  ], 
  {:name => 'Bubba Jack', :state => 'FL'} => [
    {:type => 'boat', :valuation => 12000}
  ]
]

On the JS side, I want to be able to access the individual fields, However, JS doesn’t allow the keys of objects to be objects, so data.to_json converts all the person details into one string like this:

[{"{:name=>\"Bill Smith\", :state=>\"KS\"}":[{"type":"house","valuation":150000},{"type":"car","valuation":10000}],"{:name=>\"Bubba Jack\", :state=>\"FL\"}":[{"type":"boat","valuation":12000}]}]

I can’t do stuff like

var people = Object.keys(data);
for(var i=0; i<people.length; i++) {
    var totAsset = 0;
    var person = people[i];
    for(var j=0;j<data[person].length; j++) {
        totAssset = totAsset + data[person][j].valuation
    }
    alert("got person named " +person.name+" having $"+totAsset);
}

What is the ‘right’ way to represent and use complex data on the JS side of an AJAX app?

Update: So both sgrif and Andrew’s answers provide a solution to this particular problem. I’ve upvoted both of them, but I’ll wait a few to see if there is a more general idea someone has to represent objects as keys or something with that functionality before accepting.

  • 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-10T00:41:43+00:00Added an answer on June 10, 2026 at 12:41 am

    Wouldn’t it be easier to organize your records as such:

    @people = [
        {:name => 'Bill Smith', :state => 'KS', :assets => [
            {:type => 'house', :valuation => 150000},
            {:type => 'car', :valuation => 10000}
        ]}, 
        {:name => 'Bubba Jack', :state => 'FL', :assets => [
            {:type => 'boat', :valuation => 12000}
        ]}
    ]
    

    Then use “render :json => @people”, which will give you.

    {[ "name": 'Bill Smith', "state": 'KS', "assets": [
        {"type": 'house', "valuation": 150000},
        {"type": 'car', "valuation": 10000},
    ],
    [ "name": 'Bubba Jack', "state": 'FL', "assets": [
        {"type": 'boat', "valuation": 12000}
    ]}
    

    Then access the values as such.

    'Bill Smith' = people[0]["name"]
    'house'      = people[0]["assets"][0]["type"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to send some email in my C# app. I am behind a proxy
Am trying to send a mail to recipient from iPhone app without user's interaction.
Im trying to send a mail from a asp.net script, but im getting this
Im trying to send images over HTTP using HttpListener but some images come from
I'm trying to send a GET request to a App Engine server from a
I`m trying to send simple html page with form data to user with GET,
I am trying to send emails from smtps (secure smtp) using Indy and the
I am trying to send data as a model from an html form to
I trying to send a remote form to a different subdomain (example.domain.com) from the
I'm trying to sum reduce an array from a kernel without needing to send

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.