What ruby gems and/or DSLs are available for converting SQL records into JSON objects?
-
I have some complicated SQL ( connected to SQL Server using TinyTDS) and I want to output JSON. The trick is, several SQL records map to a single JSON object. What the is absolute easiest way to do this? ActiveRecord? DataMapper? Sequel? That is, given select id, title, detail from .... I want JSON in the form { id:"123" , title:"hello", details:["world","mom","dad"] } or a ruby hash {:id=>1, :title=>"hello", :details=>["world", "mom", "dad"]} I would like this to be as lightweight as possible (i.e. minimal install and dependencies), and fast so it can run on the command line and just dump hashes or JSON hashes to a flat file. I also need it to be a bit more clever, so it can map select id, title, detail_id, detail_text, detail_label from .... to {:id=>1, :title=>"hello", :detail_ids => [321,322,323], :detail_texts=>["world", "mom", "dad"] , :detail_labels =>[a,b,c]} or perhaps a nested structure {:id=>1, :title=>"hello", :details=>[ {id:321, text:"world", label:a] , {}, {} ] or some other suitable JSON object
-
Answer:
I'm not quite sure what you're asking but I'll do my best to answer the question I want you to ask. It's all going to come down to the queries, I guess. There, it's either going to be an N+1 deal to nest all the details or a join. As for gems that will serialize JSON... You can do it yourself with the JSON gem: http://flori.github.com/json/ Sequel has a JSON serializer: http://sequel.rubyforge.org/rdoc-plugins/classes/Sequel/Plugins/JsonSerializer.html I'd probably skip ActiveRecord and DataMapper depending on how legacy your legacy database is and go with straight SQL executed within Sequel or TinyTDS.
Jeff Schoolcraft at Quora Visit the source
Other answers
I already am using tSQL / TinyTDS and can create json explicitly. Well I want a general purpose solution using DSL / ORM The point is that in most ORMs, the tutorials and examples assume that you have complete control over the DB. I have a legacy DB and I don't have access to the DB--the normal situtation in most companies. What I have access to is raw SQL. So what I need is to map the raw SQL into usable objects (hashes, ruby objects, JSON , etc)--an ORM Yes, the problem is an N+1 deal, so I would use a recursive algorithm. So all I really want to map raw SQL select sections into Objects. I can provide all the raw SQL, the join conditions, etc Here is how I posed the question on Stack Overflow -- I would like to use the ruby Sequel gem to connect to a legacy SQL Server database and retrieve complex objects that require both a join and legacy sql That is, say I have a Title with Details, where each model object / table is defined by some arbitrary complex SQL Title --> select id, title from ... and Detail --> select detail_id, title_id, text, label, ... How can I do this in Sequel so I can get a Title object with Detail records?
Charles H Martin
Related Q & A:
- What is the best practice for free space for a SQL server database drive?Best solution by Database Administrators
- how to use ajax with json in ruby on rails?Best solution by Stack Overflow
- What is the most powerful nasal decongestant available?Best solution by Yahoo! Answers
- What major U.S. products are not available in Australia?Best solution by Yahoo! Answers
- What are the current world records for swimming?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.