Created
January 15, 2012 13:28
-
-
Save tayfun/1615864 to your computer and use it in GitHub Desktop.
Pretty Print Json
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
""" | |
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