Skip to content

Instantly share code, notes, and snippets.

@willhbr
Created October 25, 2015 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willhbr/a7e2e6f517cb24a28c1b to your computer and use it in GitHub Desktop.
Save willhbr/a7e2e6f517cb24a28c1b to your computer and use it in GitHub Desktop.
Format JSON with newlines.
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