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

85 Views Asked by At

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.

2

There are 2 best solutions below

0
On

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.

0
On

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