Rails display data from XML file via respond_to format.json - ruby-on-rails

I have an XML file containing text data that I need to display to the user. I am using ember.js and therefore need to provide my response in json format.
My initial thoughts are to load the XML file and convert it to json using XSLT and then rendering this. However, I don't fully understand how the respond_to format.json method works. At a guess, I would say it turns the result of the instance variable into json, so if my data is already in json would it cause me any issues using this approach?
What is the best way to render my XML file to a view in json?

If you want to combine json and xml features, you might use rabl:
https://github.com/nesquena/rabl

Related

JSON Views in Grails

I'm migrating a Grails 2.2.5 app to Grails 4.0.2, and I have it working OK but now want to start taking advantage of new features that came in with Grails 3 and 4, such as improvements in the area of JSON responses. I've been having a play with JSON Views and the use of respond, and have some test methods working. Still struggling in some areas, though. For example, I have in multiple places in controllers code which produces a Map, which I then return via render map as JSON. This works fine, with the JSON representation of the objects in the map being determined by customer marshallers using JSON.registerObjectMarshaller(MyClass).
My understanding is that the use of respond and JSON Views for such things is now being encouraged, in preference to the render xxx as JSON approach (please correct me if I'm wrong). So two questions arise from this:
For my 'weekSummary' method in my SaleController, for this to work I presumably need to create a 'weekSummary.gson' file in /sale/views. What should that gson file look like? I've tried with e.g.
model {
Map map
}
json map
but this just renders null. How do I handle maps in JSON views?
What advantages does this give me over render xxx as JSON, given that it is a little more cumbersome (having to create a new GSON file)?

Take a string representation of JSON and render it as JSON in the broswer

I'm playing around with the Twitter api, and have gotten back super long JSON response. I saved the response as a string in a seperate file, and I want to have Chrome display that string as JSON, so I can collapse/ expand the nested parts in JSON view.
I feel like there should be an easier way to do this rather than temporarily changing my api controller in Rails...any suggestions? This is for a Rails 4 app using Backbone.js in the front end.
Ah, stupid mistake on my part -- I was using one of the referred to chrome extensions, JSONView, and asked this question after being surprised that it wasn't working.
The reason it wasn't working was because contents of the file were not actually in JSON format, they were in a ruby hash.
I was able to fix it by replacing this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs)
end
with this:
File.open('exampleResponse', 'w') do |file|
file.write(Twitter::SearchResults.new(request).attrs.to_json)
end
There are many chrome extensions to view formatted json. I use JsonView and it works fine, but I imagine there are dozens if not hundreds to choose from.

Grails controller rendering method render vs respond

I just realised that for a Grails controller there is another rendering method 'respond'.
What's the difference between respond and render method if we want to render a view in the controller.
The respond method uses content negotiation to respond with the most appropriate content type based on the requests 'ACCEPT' header.
Accept: text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8, application/json
This way the consumer of your site can choose how they wish to be returned data. This may not be the best option if you want to force a specific return type. For example: You are building a REST api and only want to return json or xml, if the user asks for test.html then they may be returned your data in a format that you do not wish to support. Otherwise respond can be an easy way to support multiple return formats without programming them each separately.
Render explicitly defines the format you wish to return your data in :
(Examples from documentation)
render Book.list(params) as JSON
render Book.get(params.id) as XML
// render with status code
render(status: 503, text: 'Failed to update book ${b.id}')
More information:
Respond: http://grails.org/doc/latest/ref/Controllers/respond.html
Render:http://grails.org/doc/latest/ref/Controllers/render.html

Using JSON with Michael hartl tutorial?

I would like to save a JSON file in the JavaScript folder and read data from it and save the data to the database. How do I read a JSON file in Ruby on Rails 4?
You should do a respond_to block in your controller, return JSON data and parse it with JavaScript in your view.
If you need to view the straight JSON data just add ".json" (www.your_url.com.json) to the end of the url in your browser and you'll see the data returned, in which case you can copy it and save it wherever you like.

In rails is there a difference between mysite/show/1.js and mysite/show/1.json

In my rails 3 app, if I want a request to return json data does it matter if I use
mysite/show/1.js
or
mysite/show/1.json
I know it seems obvious to use the json version but in my responses they look the same to me.
First of all: It depends on the way you implement the respond_to block.
With 1.json it should be clear that it delivers data as JSON.
1.js could return Javascript that is evaluated by the page that requested it. In the early Rails/Ajax days this used to be done with RJS templates. See http://www.codyfauser.com/2005/11/20/rails-rjs-templates

Resources