Is there an existing way to get json.dumps()
output to appear as "pretty" formatted JSON inside ipython notebook?
8 Answers
json.dumps
has an indent
argument, printing the result should be enough:
print(json.dumps(obj, indent=2))
-
11
-
however Chinese character messed up with Unicode in this approach– Luk AronCommented Nov 28, 2019 at 10:26
-
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.– filmorCommented Nov 28, 2019 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:
-
2
-
4it puzzles me, that this works in Jupyter Lab, but not in good old notebooks. There it calls repr_json if present.– marscherCommented Apr 29, 2021 at 19:51
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 <" Commented Dec 13, 2016 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 .– HassanCommented Dec 22, 2016 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 Commented Apr 11, 2018 at 6:14 -
1
I am just adding the expanded variable to @Kyle Barron answer:
from IPython.display import JSON
JSON(json_object, expanded=True)
-
3It does not work for me
TypeError: __init__() got an unexpected keyword argument 'expanded'
– mrvolCommented May 16, 2019 at 13:06 -
This works nicely for me. No error when using the 'expanded' keyword. My IPython.__version__ shows '7.8.0'.– DavidCommented Apr 19, 2020 at 19:26
-
5For me there is no error, but all I get is the output
<IPython.core.display.JSON object>
. No interactive pretty printing. Commented Aug 11, 2021 at 8:17 -
-
@ShnitzelKiller, you might be printing it using
print(JSON(...))
Commented Sep 30, 2023 at 15:04
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!
For Jupyter notebook, may be is enough to generate the link to open in a new tab (with the JSON viewer of firefox):
from IPython.display import Markdown
def jsonviewer(d):
f=open('file.json','w')
json.dump(d,f)
f.close()
print('open in firefox new tab:')
return Markdown('[file.json](./file.json)')
jsonviewer('[{"A":1}]')
'open in firefox new tab:
Just an extension to @filmor answer(https://stackoverflow.com/a/18873131/7018342).
This encodes elements that might not compatible with json.dumps and also gives a handy function that can be used just like you would use print.
import json
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
if isinstance(obj, np.bool_):
return bool(obj)
return super(NpEncoder, self).default(obj)
def print_json(json_dict):
print(json.dumps(json_dict, indent=2, cls=NpEncoder))
Usage:
json_dict = {"Name":{"First Name": "Lorem", "Last Name": "Ipsum"}, "Age":26}
print_json(json_dict)
>>>
{
"Name": {
"First Name": "Lorem",
"Last Name": "Ipsum"
},
"Age": 26
}
For some uses, indent should make it:
print(json.dumps(parsed, indent=2))
A Json structure is basically tree structure. While trying to find something fancier, I came across this nice paper depicting other forms of nice trees that might be interesting: https://blog.ouseful.info/2021/07/13/exploring-the-hierarchical-structure-of-dataframes-and-csv-data/.
It has some interactive trees and even comes with some code including linking to this question and the collapsing tree from Shankar ARUL.
Other samples include using plotly Here is the code example from plotly:
import plotly.express as px
fig = px.treemap(
names = ["Eve","Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents = ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve"]
)
fig.update_traces(root_color="lightgrey")
fig.update_layout(margin = dict(t=50, l=25, r=25, b=25))
fig.show()
And using treelib. On that note, This github also provides nice visualizations. Here is one example using treelib:
#%pip install treelib
from treelib import Tree
country_tree = Tree()
# Create a root node
country_tree.create_node("Country", "countries")
# Group by country
for country, regions in wards_df.head(5).groupby(["CTRY17NM", "CTRY17CD"]):
# Generate a node for each country
country_tree.create_node(country[0], country[1], parent="countries")
# Group by region
for region, las in regions.groupby(["GOR10NM", "GOR10CD"]):
# Generate a node for each region
country_tree.create_node(region[0], region[1], parent=country[1])
# Group by local authority
for la, wards in las.groupby(['LAD17NM', 'LAD17CD']):
# Create a node for each local authority
country_tree.create_node(la[0], la[1], parent=region[1])
for ward, _ in wards.groupby(['WD17NM', 'WD17CD']):
# Create a leaf node for each ward
country_tree.create_node(ward[0], ward[1], parent=la[1])
# Output the hierarchical data
country_tree.show()
I have, based on this, created a function to convert json to a tree:
from treelib import Node, Tree, node
def json_2_tree(o , parent_id=None, tree=None, counter_byref=[0], verbose=False, listsNodeSymbol='+'):
if tree is None:
tree = Tree()
root_id = counter_byref[0]
if verbose:
print(f"tree.create_node({'+'}, {root_id})")
tree.create_node('+', root_id)
counter_byref[0] += 1
parent_id = root_id
if type(o) == dict:
for k,v in o.items():
this_id = counter_byref[0]
if verbose:
print(f"tree.create_node({str(k)}, {this_id}, parent={parent_id})")
tree.create_node(str(k), this_id, parent=parent_id)
counter_byref[0] += 1
json_2_tree(v , parent_id=this_id, tree=tree, counter_byref=counter_byref, verbose=verbose, listsNodeSymbol=listsNodeSymbol)
elif type(o) == list:
if listsNodeSymbol is not None:
if verbose:
print(f"tree.create_node({listsNodeSymbol}, {counter_byref[0]}, parent={parent_id})")
tree.create_node(listsNodeSymbol, counter_byref[0], parent=parent_id)
parent_id=counter_byref[0]
counter_byref[0] += 1
for i in o:
json_2_tree(i , parent_id=parent_id, tree=tree, counter_byref=counter_byref, verbose=verbose,listsNodeSymbol=listsNodeSymbol)
else: #node
if verbose:
print(f"tree.create_node({str(o)}, {counter_byref[0]}, parent={parent_id})")
tree.create_node(str(o), counter_byref[0], parent=parent_id)
counter_byref[0] += 1
return tree
Then for example:
import json
json_2_tree(json.loads('{"2": 3, "4": [5, 6]}'),verbose=False,listsNodeSymbol='+').show()
gives:
+
├── 2
│ └── 3
└── 4
└── +
├── 5
└── 6
While
json_2_tree(json.loads('{"2": 3, "4": [5, 6]}'),listsNodeSymbol=None).show()
Gives
+
├── 2
│ └── 3
└── 4
├── 5
└── 6
As you see, there are different trees one can make depending on how explicit vs. compact he wants to be. One of my favorites, and one of the most compact ones might be using yaml:
import yaml
j = json.loads('{"2": "3", "4": ["5", "6"], "7": {"8": "9"}}')
print(yaml.dump(j, sort_keys=False))
Gives the compact and unambiguous:
'2': '3'
'4':
- '5'
- '6'
'7':
'8': '9'
pprint()
to print the JSON (from pprint import pprint
)