Rails - How can I display JSON indentation well?

I have a controller action that returns JSON data for api purposes and a lot of it. I want to be able to test it in a browser and make it beautifully indented for the viewer. For example, if my data

data = { :person => { :id => 1, :name => "john doe", :age => 30 }, :person => ... }

I want to see

{ "person" : 
    { 
        "id"   : 1, 
        "name" : "john doe",
        "age"  : 30,
    }, 

   "person" : 
    { 
        "id"   : 2, 
        "name" : "jane doe",
        "age"  : 31,
    },

    ...etc
}

In view.

I thought about using different routes to get voluminous / pretty data:

# GET /api/json
# ...
respond_to do |format|
  format.html { render :json => data.to_json }
end

# GET /api/json/inspect
# ...
respond_to do |format|
  format.html { render :text => pretty_json }
end

Does anyone know of a gem / plugin that does this or something similar? I tried using JSON.pretty_generate, but it does not work inside the rails (2.3.5). thank.

+5
source share
4 answers

Firefox, , , json, , JSONview Firefox ( ):

https://addons.mozilla.org/en-US/firefox/addon/10869/

json , .

+3

render json: JSON.pretty_generate(data)
+3

jsonpp - ( ) JSON.

JSON :

curl -s -L http://t.co/tYTq5Pu | jsonpp
0

All Articles