Is there an existing way to get json.dumps()
output to appear as "pretty" formatted JSON inside ipython notebook?
json.dumps
has an indent
argument, printing the result should be enough:
print(json.dumps(obj, indent=2))
-
9
-
-
1That's simply not true. Python's
json.dumps
defaults toensure_ascii=True
which escapes Chinese (or actually: any non-ASCII) code-point. Useprint(json.dumps(obj, indent=2, ensure_ascii=False))
and you'll get them unescaped. – filmor Nov 28 '19 at 13:41
This might be slightly different than what OP was asking for, but you can do use IPython.display.JSON
to interactively view a JSON/dict
object.
from IPython.display import JSON
JSON({'a': [1, 2, 3, 4,], 'b': {'inner1': 'helloworld', 'inner2': 'foobar'}})
Edit: This works in Hydrogen and JupyterLab, but not in Jupyter Notebook or in IPython terminal.
Inside Hydrogen:
import uuid
from IPython.display import display_javascript, display_html, display
import json
class RenderJSON(object):
def __init__(self, json_data):
if isinstance(json_data, dict):
self.json_str = json.dumps(json_data)
else:
self.json_str = json_data
self.uuid = str(uuid.uuid4())
def _ipython_display_(self):
display_html('<div id="{}" style="height: 600px; width:100%;"></div>'.format(self.uuid), raw=True)
display_javascript("""
require(["https://rawgit.com/caldwell/renderjson/master/renderjson.js"], function() {
document.getElementById('%s').appendChild(renderjson(%s))
});
""" % (self.uuid, self.json_str), raw=True)
To ouput your data in collapsible format:
RenderJSON(your_json)
Copy pasted from here: https://www.reddit.com/r/IPython/comments/34t4m7/lpt_print_json_in_collapsible_format_in_ipython/
-
I cannot get it to work on Jupyter 1.0. Any idea why ? Javascript console says : "SyntaxError: Unexpected token <" – user474491 Dec 13 '16 at 10:59
-
There is a problem in the code. Line 10 should be changed to
self.json_str = json_data
. It solves the problem reported by @user474491 . – Hassan Dec 22 '16 at 9:51 -
-
1I've update a few issues and got this to work in the latest Jupyter Notebook, including @user474491 's and @Hassan 's suggestions, I also added an explicit call to
_ipython_display_()
in the__init__()
to ensure that we can safely interleave calls to python's nativeprint()
andRenderJSON()
and still get this to work gist.github.com/t27/48b3ac73a1479914f9fe9383e5d45325 – Tarang Shah Apr 11 '18 at 6:14 -
It will be even better if this accepts
list
type, sincecaldwell/renderjson
is totally capable of rendering json arrays – Huy Le Jul 17 '18 at 10:15
I am just adding the expanded variable to @Kyle Barron answer:
from IPython.display import JSON
JSON(json_object, expanded=True)
I found this page looking for a way to eliminate the literal \n
s in the output. We're doing a coding interview using Jupyter and I wanted a way to display the result of a function real perty like. My version of Jupyter (4.1.0) doesn't render them as actual line breaks. The solution I produced is (I sort of hope this is not the best way to do it but...)
import json
output = json.dumps(obj, indent=2)
line_list = output.split("\n") # Sort of line replacing "\n" with a new line
# Now that our obj is a list of strings leverage print's automatic newline
for line in line_list:
print line
I hope this helps someone!