IPython Documentation

Table Of Contents

Previous topic

Module: utils.ipstruct

Next topic

Module: utils.module_paths

This Page

Warning

This documentation is for an old version of IPython. You can find docs for newer versions here.

Module: utils.jsonutil

Utilities to manipulate JSON objects.

6 Functions

IPython.utils.jsonutil.rekey(dikt)

Rekey a dict that has been forced to use str keys where there should be ints by json.

IPython.utils.jsonutil.extract_dates(obj)

extract ISO8601 dates from unpacked JSON

IPython.utils.jsonutil.squash_dates(obj)

squash datetime objects into ISO8601 strings

IPython.utils.jsonutil.date_default(obj)

default function for packing datetime objects in JSON.

IPython.utils.jsonutil.encode_images(format_dict)

b64-encodes images in a displaypub format dict

Perhaps this should be handled in json_clean itself?

Parameters:

format_dict : dict

A dictionary of display data keyed by mime-type

Returns:

format_dict : dict

A copy of the same dictionary, but binary image data (‘image/png’ or ‘image/jpeg’) is base64-encoded.

IPython.utils.jsonutil.json_clean(obj)

Clean an object to ensure it’s safe to encode in JSON.

Atomic, immutable objects are returned unmodified. Sets and tuples are converted to lists, lists are copied and dicts are also copied.

Note: dicts whose keys could cause collisions upon encoding (such as a dict with both the number 1 and the string ‘1’ as keys) will cause a ValueError to be raised.

Parameters:

obj : any python object

Returns:

out : object

A version of the input which will not cause an encoding error when encoded as JSON. Note that this function does not encode its inputs, it simply sanitizes it so that there will be no encoding errors later.

Examples

>>> json_clean(4)
4
>>> json_clean(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> sorted(json_clean(dict(x=1, y=2)).items())
[('x', 1), ('y', 2)]
>>> sorted(json_clean(dict(x=1, y=2, z=[1,2,3])).items())
[('x', 1), ('y', 2), ('z', [1, 2, 3])]
>>> json_clean(True)
True