Registry / serialization / compact-json

compact-json

JSON →
library1.8.2pypypi✓ verified 86d ago

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.

pip install compact-json
INSTALL
IMPORT
SIG · COMPACT-JSON
C
compact-json
serializationpythonv1.8.2
Install
2.1s avg
Import
187ms
Disk
25MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.8.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.194s · 25.3MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.1s · import 0.180s · 26MB
25MB installed
● package 25MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Formatter
from compact_json import Formatter
EolStyle
from compact_json import EolStyle

Demonstrates basic usage of the `Formatter` class to serialize a Python dictionary into a compact JSON string with custom formatting options. The example uses a simple dictionary and prints the formatted output to the console.

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))
Debug
Known issues
breakingThe `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.
fix
Migrate to `fractured-json`. The API for `fractured-json` is similar but may require adjustments to imports and configuration options.
affects: >=1.8.0
gotchaWhen 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`.
fix
Ensure all dictionary keys are strings before passing the object to `Formatter.serialize()` or `Formatter.dump()` if key coercion is undesirable. Review `RuntimeWarning` messages carefully.
affects: All
gotchaThe 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.
fix
Experiment with formatter properties (e.g., `formatter.max_inline_complexity`, `formatter.indent_spaces`, `formatter.max_total_line_length`) to fine-tune the output format for your specific JSON data. Consult the GitHub README for a full list of configuration options.
affects: All
Errors
Common errors & fixes
/path/src/compact_json/formatter.py:XXX: RuntimeWarning: coercing key value XXX to string
A Python dictionary with non-string keys (e.g., integers, booleans) was passed to the formatter. JSON requires all object keys to be strings.
fix
Before 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
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.
fix
Pre-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).
Upgrade
Version history
1.8.2latest on PyPI · released Dec 26, 2025
Audit
Dependencies
importlib-resourcesoptionalProvides backport of `importlib.resources` for Python <3.9
setuptoolsrequiredStandard Python packaging tools
wcwidthrequiredCalculates printable width of unicode strings, used for formatting alignment
Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources
compact-json — pip install compact-json · libregistry