0

I've got a JSON hash of hashes returned by a website API that I want to parse and display based on a specific key's value within the internal hashes.

I can think of solutions that would achieve this, but they would take a number of lines of code and don't seem efficient. Surely there must be a way to natively in Rails, given the focus on convention over configuration. I've googled around a bit, but found nothing that covers this issue.

Sample Response from API:

[{"banner":"01197271","birthday":"1991-01-11","committee_id":1,"created_at":"2012-08-08T01:56:02-05:00","email":"me@example.com","first_name":"Dan","graduation_date":"May 2013","hometown":"San Antonio","hours_enrolled":15,"id":2,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"Tester","local_apt":"","local_city":"San Antonio","local_state":"Texas","local_street":"One UTSA Circle","local_zip":"78249","major":"Computer Science","permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"One UTSA Circle","permanent_zip":"","phone":"5558813284","same_address":true,"tour_trained":false,"updated_at":"2012-08-17T03:35:26-05:00","utsa_id":"uoi431"},
{"banner":"","birthday":"1990-10-25","committee_id":null,"created_at":"2012-08-03T16:19:23-05:00","email":"you@example.com","first_name":"Test","graduation_date":null,"hometown":null,"hours_enrolled":null,"id":1,"image":{"url":null,"thumb":{"url":null},"large":{"url":null}},"invitation_accepted_at":null,"invitation_limit":null,"invitation_sent_at":null,"invitation_token":null,"invited_by_id":null,"invited_by_type":null,"last_name":"User","local_apt":"","local_city":"","local_state":"","local_street":"","local_zip":"","major":null,"permanent_apt":"","permanent_city":"","permanent_state":"","permanent_street":"","permanent_zip":"","phone":"","same_address":false,"tour_trained":false,"updated_at":"2012-08-15T10:05:54-05:00","utsa_id":""}]

Potential solution would be to go through each internal hash, determining value of relevant key value, then store, based on where the key value places it compared to already tested hashes. When complete, return.

Dan
  • 3,246
  • 1
  • 32
  • 52
  • 2
    Post some of your solutions and also an example of that api response. – Sergio Tulentsev Aug 17 '12 at 08:34
  • As you wish, @SergioTulentsev. – Dan Aug 17 '12 at 08:39
  • Requests have been added, but I'm not convinced that specifics are relevant to a valid solution, given that I'm asking for a best practice when handling problems like this one. – Dan Aug 17 '12 at 08:47
  • I mean, post some code. This is site for programmers, isn't it? Also, you may want to format api response so that it doesn't need horizontal scrolling. It's almost impossible to comprehend. – Sergio Tulentsev Aug 17 '12 at 08:47
  • I think, I still don't fully understand the question. Do you want to *iterate* in a specific order? Why does it matter? What is it that you ultimately want to do? Display in order? – Sergio Tulentsev Aug 17 '12 at 08:49
  • Why would I post code? If someone is able to answer the question, seeing my code has no baring on what their answer should be. I see no point taking time writing out code that I've already stated I don't believe is the best way to approach the problem. – Dan Aug 17 '12 at 08:49
  • 1
    Create an ActiveResource object and add relevant methods to it. – apneadiving Aug 17 '12 at 08:50
  • 2
    Natural language is ambiguous. Code is not. Just saying. – Sergio Tulentsev Aug 17 '12 at 08:50
  • @SergioTulentsev: Yes. The end goal is to index the values based on a logical order which can only be determined by values inside the internal hash. – Dan Aug 17 '12 at 08:51
  • @apneadiving, that's a really good point. I don't know why I didn't think of that. I'm just going to do that. If you post as answer then I'll accept it. – Dan Aug 17 '12 at 08:52
  • Can you supply a desired output for aforementioned input? – Sergio Tulentsev Aug 17 '12 at 08:52
  • I agree with @SergioTulentsev. Everyone can *guess* what you intend, but it might not be the way you want. You can think of a solution, then, paste the code, then people can understand your intention well enough. – shigeya Aug 17 '12 at 08:53
  • @Dan: just posted as answer then :) – apneadiving Aug 17 '12 at 08:58

2 Answers2

0

Ok so if you have objects that are set up to parse this information, those objects can build themselves based off the parameters of your hash. So you could do something like this

object = MyObject.create(your_hash_parameters)

Where your_hash_parameters are the parameters that you presented in your example.

I'm not sure what would happen if there were more paramaters than your object knew what to do with, if it would still build itself or not. If that is the case, you could use the delete_if method to exclude attributes that are unwanted.

One more note, if this isn't something that you want saved to your database, and its only to display temporary information. I would set up a model with attr_accessors that represent the attributes that you are displaying.

Trip
  • 26,756
  • 46
  • 158
  • 277
  • Thanks, @Trip. I should have thought of using the database for this. – Dan Aug 17 '12 at 08:58
  • 1
    Right, and also note that you don't need to technically use the DB. They can just be temporary attribute accessors that represent the attributes you want to parse the data. ( last paragraph ) – Trip Aug 17 '12 at 08:59
0

As told in comment, I'd create an ActiveResource object and add relevant methods to it.

apneadiving
  • 114,565
  • 26
  • 219
  • 213