Created
October 25, 2015 22:02
-
-
Save willhbr/a7e2e6f517cb24a28c1b to your computer and use it in GitHub Desktop.
Format JSON with newlines.
This file contains 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
def self.pretty_json(o, l=0) | |
o.class == Hash ? "{\n" + o.keys.map { |k| (" " * (l + 1) * INDENT) + "\"#{k.to_s}\": " + pretty_json(o[k], l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "}" : o.class == Array ? "[\n" + o.map { |k| (" " * (l + 1) * INDENT) + pretty_json(k, l + 1) }.join(",\n") + "\n" + (" " * l * INDENT) + "]" : o.inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment