Compact JSON Formatter
compact-json is a Python library that provides a configurable JSON formatter, producing compact yet human-readable output. It is a pure Python port of FracturedJsonJs. As of its latest version, 1.8.2, the package is deprecated, and users are strongly encouraged to migrate to the `fractured-json` library, which directly tracks the original FracturedJson .NET assembly. The library is currently in maintenance mode with no further development planned.
Common errors
-
/path/src/compact_json/formatter.py:XXX: RuntimeWarning: coercing key value XXX to string
cause A Python dictionary with non-string keys (e.g., integers, booleans) was passed to the formatter. JSON requires all object keys to be strings.fixBefore formatting, ensure all keys in your Python dictionaries are strings. For example, explicitly convert integer keys to strings: `{str(k): v for k, v in my_dict.items()}`. -
TypeError: Object of type <non-serializable-type> is not JSON serializable
cause The input Python object contains elements (e.g., `datetime` objects, `set`s, custom classes) that cannot be directly converted to JSON by the underlying `json` module.fixPre-process your Python object to convert non-serializable types into JSON-compatible types (e.g., `datetime` to ISO 8601 strings, `set`s to `list`s). Alternatively, provide a custom `default` function to the underlying `json.dumps` call, if `compact-json` exposes such a mechanism (check the `Formatter`'s `serialize` method documentation).
Warnings
- breaking The `compact-json` library is officially deprecated. Users are strongly advised to migrate to `fractured-json` for ongoing support and new features. `compact-json` will receive no further development and has been archived on PyPI.
- gotcha When dictionary keys are not strings (e.g., integers), `compact-json` will coerce them to strings to ensure valid JSON, issuing a `RuntimeWarning`. This differs from the built-in `json` module's default behavior, which may raise a `TypeError`.
- gotcha The output JSON's compactness and readability are highly dependent on formatter options like `max_inline_complexity`, `indent_spaces`, and `max_total_line_length`. Default settings might not produce the desired level of compactness for all data structures.
Install
-
pip install compact-json
Imports
- Formatter
from compact_json import Formatter
- EolStyle
from compact_json import EolStyle
Quickstart
import json
from compact_json import Formatter, EolStyle
# Example Python dictionary
data = {
"widget": {
"debug": "on",
"window": {"title": "Sample Widget", "name": "main_window", "width": 500, "height": 500},
"image": {"src": "Images/Sun.png", "name": "sun1", "hOffset": 250, "vOffset": 250, "alignment": "center"},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}
formatter = Formatter()
formatter.indent_spaces = 2
formatter.max_inline_complexity = 2 # Low complexity to demonstrate compactness
formatter.json_eol_style = EolStyle.LF
formatter.simple_bracket_padding = False # To make it more compact
formatted_json_string = formatter.serialize(data)
print(formatted_json_string)
# Example of customizing formatter options
another_formatter = Formatter()
another_formatter.use_tab_to_indent = True
another_formatter.max_total_line_length = 60
print('\n--- Formatted with tabs and shorter lines ---')
print(another_formatter.serialize(data))