prettify JSON output of active-model-serializer in rails console
Asked Answered
T

1

16

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"?

Telugu answered 25/2, 2014 at 0:8 Comment(0)
C
29

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.

Crater answered 26/2, 2014 at 12:42 Comment(1)
I am going to use this way for a situation where i need to add serialized object as json in a json which has some other data too, and which is being return as render i.e for json response. I don't know if this is a bad idea but it seems it will work :)Wilmawilmar

© 2022 - 2024 — McMap. All rights reserved.