Module contents

class json_lint_tools.Diff(filepath: str = '', fileobj: IO = None, indent: int = 2, sort_arrays: bool = False, sort_keys: bool = False)

Bases: object

Creates an object that can compares a JSON string to what would be created using json.dumps and provides tools for using as a cli tool.

Parameters:
  • filepath – Optional relative or absolute path to a file to work with. You must provide one of filepath or fileobj.

  • fileobj – Optional file or file-like object, such as the return of open() or a StringIO object. Must implement read, readlines, and seek. You must provide one of filepath or fileobj.

  • indent – Number of indentions to check against

  • sort_arrays – If True values in all arrays in the JSON will be sorted alphabetically.

  • sort_keys – If True keys in all objects in the JSON will be sorted alphabetically

abspath: str

Absolute path to file. Will be an empty string with using fileobj

format_file() None

Will write the formatted JSON back to the original file if working with a file path. When working with a file or file-like object the formatted JSON will be printed to stdout

formatted_data: str

The input JSON after being formatted

get_diff() List[str]

Return a diff to be used as visual output as an list of strings using the difftools.unified_diff packagge

property is_diff: bool

Check if the input would be changed at all

print() None

Print the pretty formatted JSON to stdout

print_diff(color: bool = True) None

Prints the diff output to stdout, optionally using term colors

Parameters:

color – Optionally print with color. Red for deletions and green for additions

class json_lint_tools.TermColors

Bases: object

Simple way of referencing ANSI color escapes for terminal output

END = '\x1b[0m'

End ANSI colors

GREEN = '\x1b[92m'

ANSI Green

RED = '\x1b[91m'

ANSI Red

WHITE = '\x1b[97m'

ANSI White