How to "pretty" format JSON output in Ruby on Rails How to "pretty" format JSON output in Ruby on Rails json json

How to "pretty" format JSON output in Ruby on Rails

Use the pretty_generate() function, built into later versions of JSON. For example:

require 'json'my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" }puts JSON.pretty_generate(my_object)

Which gets you:

{  "array": [    1,    2,    3,    {      "sample": "hash"    }  ],  "foo": "bar"}

The <pre> tag in HTML, used with JSON.pretty_generate, will render the JSON pretty in your view. I was so happy when my illustrious boss showed me this:

<% if @data.present? %>   <pre><%= JSON.pretty_generate(@data) %></pre><% end %>

Thanks to Rack Middleware and Rails 3 you can output pretty JSON for every request without changing any controller of your app. I have written such middleware snippet and I get nicely printed JSON in browser and curl output.

class PrettyJsonResponse  def initialize(app)    @app = app  end  def call(env)    status, headers, response =    if headers["Content-Type"] =~ /^application\/json/      obj = JSON.parse(response.body)      pretty_str = JSON.pretty_unparse(obj)      response = [pretty_str]      headers["Content-Length"] = pretty_str.bytesize.to_s    end    [status, headers, response]  endend

The above code should be placed in app/middleware/pretty_json_response.rb of your Rails project.And the final step is to register the middleware in config/environments/development.rb:

config.middleware.use PrettyJsonResponse

I don't recommend to use it in production.rb. The JSON reparsing may degrade response time and throughput of your production app. Eventually extra logic such as 'X-Pretty-Json: true' header may be introduced to trigger formatting for manual curl requests on demand.

(Tested with Rails 3.2.8-5.0.0, Ruby 1.9.3-2.2.0, Linux)