Skip to main content

Stdlib function to jsonify almost any Python object recursively

Project description

jsonify-objects

Recursively convert almost any Python object to a JSON-serializable structure.

Installation

pip install jsonify-objects

Usage

dumps

Drop-in wrapper around json.dumps that handles non-serializable types.

from jsonify_objects import dumps

dumps({"a": 1, "b": True})
# '{\n  "a": "1",\n  "b": "true"\n}'

jsonify_objects

Returns the converted structure without serializing to a string.

from jsonify_objects import jsonify_objects

jsonify_objects({"a": 1, "b": True})
# {"a": "1", "b": "true"}

Behavior

Primitives

Primitive values inside plain dicts and lists are converted to strings.

Type Output
int, float str(value)
bool "true" / "false"
str unchanged
None null
Enum str(value.value)
dumps({"x": 42, "flag": True, "color": Color.RED})
# {"x": "42", "flag": "true", "color": "red"}

Dataclasses and objects

By default, non-serializable objects fall back to repr.

dumps({"user": UserDataclass("Alice", 30)})
# {"user": "UserDataclass(name='Alice', age=30)"}

Pass serialize_objects=True to expand objects and dataclasses into dicts. Numeric fields on expanded objects preserve their native JSON types.

dumps({"user": UserDataclass("Alice", 30)}, serialize_objects=True)
# {"user": {"name": "Alice", "age": 30}}

Nested objects are expanded recursively.

dumps({"person": Person("Bob", Address("NYC"))}, serialize_objects=True)
# {"person": {"name": "Bob", "address": {"city": "NYC"}}}

Objects without a __dict__ (e.g. slots-only classes) fall back to repr.

indent

dumps accepts an indent parameter (default 2) passed through to json.dumps.

dumps({"a": 1}, indent=4)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonify_objects-1.0.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsonify_objects-1.0.0-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file jsonify_objects-1.0.0.tar.gz.

File metadata

  • Download URL: jsonify_objects-1.0.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for jsonify_objects-1.0.0.tar.gz
Algorithm Hash digest
SHA256 9321a630e95e77bbb5df9a523879c0349991f1ee2e3f5a6f3292e6085a95d424
MD5 8a52f3831816c1b2e7ac24194a1b95dd
BLAKE2b-256 03f673ae7fbf7fec71aec8e8b2982bd51f77cd988092f20caef3b533ffa04dbe

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonify_objects-1.0.0.tar.gz:

Publisher: publish.yml on BlakeJC94/jsonify-objects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file jsonify_objects-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonify_objects-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3fcf84e7701db0a32f2f1f2d1ba49e839b651b51ee0b1dedadc2a4efaa294ba
MD5 eb42c3c632123cdf4af7751544b490dc
BLAKE2b-256 08a111ecb0118178606980dba8d32a772e3ea31f243221bd3074f1bdb5c10877

See more details on using hashes here.

Provenance

The following attestation bundles were made for jsonify_objects-1.0.0-py3-none-any.whl:

Publisher: publish.yml on BlakeJC94/jsonify-objects

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page