Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
python [2016/10/10 16:41] gevatterpython [2016/10/10 16:42] (current) gevatter
Line 17: Line 17:
 </code> </code>
 source: http://stackoverflow.com/a/13752442 source: http://stackoverflow.com/a/13752442
 +
 +===== json debugging, colored output =====
 +<code>
 +#!/usr/bin/python
 +
 +import json
 +import requests
 +from pprint import pprint
 +from pygments import highlight
 +from pygments.lexers import PythonLexer
 +from pygments.formatters import Terminal256Formatter
 +from pprint import pformat
 +
 +def pprint_color(obj):
 +    print highlight(pformat(obj), PythonLexer(), Terminal256Formatter())
 +
 +data = {
 +    u'type': u'requiesttype',
 +    u'searchkey': 'string that is searched for'}
 +
 +url = 'http://url.to/submit/to'
 +
 +r = requests.post(
 +    url,
 +    data=json.dumps(data))
 +
 +try:
 +    pprint_color(
 +        json.loads(r.text))
 +except ValueError:
 +    print r.text
 +</code>