-
Notifications
You must be signed in to change notification settings - Fork 0
Home
brianburwell11 edited this page Jun 21, 2022
·
7 revisions
Generate prettier and more compact JSON dumps
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@masterpoetry add git+ssh://git@github.com/brianburwell11/prettier-json.git#masterbasictype2str(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