Skip to content
brianburwell11 edited this page Jun 21, 2022 · 7 revisions

Module prettier_json

prettier-json

made-with-python Code style: black Github release Documentation

Generate prettier and more compact JSON dumps

Installation

prettier_json can be installed directly from the GitHub repository using one of these commands:

pip install git+ssh://git@github.com/brianburwell11/prettier-json.git@master
poetry add git+ssh://git@github.com/brianburwell11/prettier-json.git#master

Usage

Functions

basictype2str(obj) : Convert a python object to its string representation

Parameters
----------
obj : any
    Any python object. If the object is not a basic type (str, bool, None), it must have a `__str__()` method.

Returns
-------
str
    A string representation of the given `obj`.

getsubitems(obj, itemkey, islast, maxlinelength, indent) : Get all of the items within an iterable object

Parameters
----------
obj : any
itemkey : str
    The key for the
islast : bool
    Whether or not `obj` is the last item in the JSON object.
maxlinelength : int
    How many characters to limit a single line to before wrapping to the next line. Default is 80 characters
indent : int
    How many spaces to use as an indent before nested keys. Default is 2

Returns
-------
list[str]
    All of the individual items in the obj
bool
    Whether or not `obj` should be rendered on a single line

indentitems(items, indent, level) : Recursively traverses the list of json lines, adds indentation based on the current depth

Parameters
----------
items : iterable
indent : int
    How many spaces to use to differentiate between levels
level : int
    How many `indent`s to place before the given item

Returns
-------
str
    The `items` iterable as an indented JSON-style string

prettyjson(obj, indent=2, maxlinelength=80) : Renders JSON content with indentation and line splits/concatenations to fit maxlinelength.

Only dicts, lists and basic types are supported

Parameters
----------
obj : dict, list, tuple, str
    A python object to be converted to JSON
indent : int, optional
    How many spaces to use as an indent before nested keys. Default is 2
maxlinelength : int, optional
    How many characters to limit a single line to before wrapping to the next line. Default is 80 characters

Returns
-------
str
    A representation of the JSON to be piped to a .json file

Clone this wiki locally