Created
August 19, 2010 12:44
-
-
Save georgkreimer/537794 to your computer and use it in GitHub Desktop.
pretty print json in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'json' | |
my_json = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar" } | |
puts JSON.pretty_generate(my_json) | |
Which gets you... | |
{ | |
"array": [ | |
1, | |
2, | |
3, | |
{ | |
"sample": "hash" | |
} | |
], | |
"foo": "bar" | |
} |
Yes, I think this one is good.
Suppose I have an original json-format string: json_string
.
require 'json'
json_string = '{"a": 1, "b":2}'
JSON.generate(JSON.parse(json_string)) # This will generate a pretty formatted one.
@kevinnio If you want to put it into a <code></code>
block in Rails, then just do it, that't the Rails stuff.
If you also want some highlight, then you can try highlight.js.
A+ 🌈💯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, but what if I want to put it into an HTML
code
block?