Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prettify JSON output of active-model-serializer in rails console

I am testing active-model-serializer output in the rails console and I am looking for a way to prettify the output. The the only solution I have found so far is:

ap JSON.parse(ProfileSerializer.new(p).to_json)

That seems like a roundabout approach. Is there a "better way"?

like image 759
errata Avatar asked Feb 25 '14 00:02

errata


1 Answers

This should do the trick:

puts JSON.pretty_generate(ProfileSerializer.new(p).serializable_hash)

That way you don't:

  1. generate a JSON string, then
  2. parse it back, then
  3. output it

but just generate a prettified JSON string.

like image 119
awendt Avatar answered Nov 15 '22 11:11

awendt