0

I'm trying to print JSON data in a view in a 'pretty' human readable format. I have a controller:

def show
  h = JSON.parse(RestClient.get("http://link_to_get_json"))
  @json = JSON.pretty_generate(h)
end

and a simple view:

= @json

But all I see, when I load the page, is the same JSON I've got, not formatted. What do I do wrong?

2 Answers 2

2

JSON.pretty_generate inserts whitespace into the returned string.

If your'e dumping the string into an HTML document, all whitespace (such as newlines) is ignored, and rendered as a single space. In order to preserve whitespace, you need add a white-space: pre CSS style, or wrap the content in a <pre> tag.

Sign up to request clarification or add additional context in comments.

2 Comments

HTML embedded in the JSON is rendered in the <pre> block nonetheless. Would suggest: @json = JSON.pretty_generate(CGI::escapeHTML(h))
@dennis that’s not relevant to this question
0

I think you were using <p>. wrap it up in <pre>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.