0

I am using JBuilder as a JSON DSL. I have it working properly but cannot figure out how to pretty print JSON.

I want to use JSON.pretty_generate but that requires a string or a hash, and JBuilder is a custom JBuilder object that does not convert.

Any ideas how to pretty print JSON with JBuilder template?

Charles
  • 50,943
  • 13
  • 104
  • 142
darksky
  • 20,411
  • 61
  • 165
  • 254
  • 1
    a solution/workaround is available here: http://stackoverflow.com/questions/13128485/pretty-print-json-generated-with-a-jbuilder-template-in-rails-3-2-8 – Jacob Brown Jun 20 '13 at 15:13

2 Answers2

0

Try parsing the JBuilder output, then using JSON.pretty_generate on that resulting output

json_builder = Jbuilder.encode do |json|
  ... your json.data ...
end

json_obj = JSON.parse(json_builder)
puts       JSON.pretty_generate(json_obj)
xycmu
  • 66
  • 1
  • 4
0

I use pretty output for all my JSON in development by using an initializer for multi_json (the current backend for jbuilder):

In config/initializers/multi_json.rb

require 'multi_json'

MultiJson.dump_options = { pretty: true } if Rails.env.development?
Tim Krins
  • 3,134
  • 1
  • 23
  • 25