Skip to content

Instantly share code, notes, and snippets.

@tayfun
Created January 15, 2012 13:28
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 tayfun/1615864 to your computer and use it in GitHub Desktop.
Save tayfun/1615864 to your computer and use it in GitHub Desktop.
Pretty Print Json
"""
Added by tayfunsen to pretty print json strings.
Put this in dist-packages (ex.
/usr/local/lib/python2.7/dist-packages/JsonExtensions.py ) and use prettys to
pretty print json strings.
Ex.
>>> resp.content
'{"comments": {"list_endpoint": "/api/v1/comments/", "schema": "/api/v1/comments/schema/"}, "groups": {"list_endpoint": "/api/v1/groups/", "schema": "/api/v1/groups/schema/"}, "posts": {"list_endpoint": "/api/v1/posts/", "schema": "/api/v1/posts/schema/"}, "profiles": {"list_endpoint": "/api/v1/profiles/", "schema": "/api/v1/profiles/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}'
>>> json.prettys(resp.content)
{
"comments": {
"list_endpoint": "/api/v1/comments/",
"schema": "/api/v1/comments/schema/"
},
"groups": {
"list_endpoint": "/api/v1/groups/",
"schema": "/api/v1/groups/schema/"
},
"posts": {
"list_endpoint": "/api/v1/posts/",
"schema": "/api/v1/posts/schema/"
},
"profiles": {
"list_endpoint": "/api/v1/profiles/",
"schema": "/api/v1/profiles/schema/"
},
"users": {
"list_endpoint": "/api/v1/users/",
"schema": "/api/v1/users/schema/"
}
}
"""
import json
def prettys(json_as_string):
print json.dumps(json.loads(json_as_string), sort_keys=True, indent=4)
json.prettys = prettys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment