prettify JSON output of active-model-serializer in rails console
-
20-12-2019 - |
Question
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"?
Solution
This should do the trick:
puts JSON.pretty_generate(ProfileSerializer.new(p).serializable_hash)
That way you don't:
- generate a JSON string, then
- parse it back, then
- output it
but just generate a prettified JSON string.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow