jsonobject is a Python library for handling deeply nested JSON objects as well-schema'd Python objects. It provides a declarative way to define data models for JSON structures, facilitating easy serialization and deserialization between Python objects and JSON. Maintained by Dimagi, it is currently at version 2.3.1 and typically follows an active release cadence.
pip install jsonobjectVerified import paths — ran on the pinned version, not inferred.
Define a `JsonObject` subclass with various `Property` types, instantiate objects, and serialize them to JSON. Also demonstrates deserialization from a dictionary.
Review code that relies on `ListProperty` raising `BadValueError` for iterable value types and adjust expectations for receiving a plain Python list instead.
Ensure your project runs on Python 3.7 or newer. Upgrade your Python environment or pin `jsonobject<1.0.0` if you must remain on older Python versions (though this is not recommended).
Always use a `lambda` or a callable for mutable default values, e.g., `ListProperty(default=list)` or `ListProperty(default=lambda: [])`.
Be mindful of which library you are using. `jsonobject` offers powerful schema validation and object-oriented access, while the standard `json` module is for raw string-to-dict/list conversion.
Ensure that your dictionary is correctly passed to the `JsonObject` constructor: `my_object = MyJsonObject(my_dict_data)`.
Check the schema definition for the property (`e.g., IntegerProperty()`) and ensure the data being assigned or provided conforms to that type. Explicitly convert data types if necessary before assignment.
Validate your JSON string using a linter or online tool before passing it to `jsonobject`. Common fixes include ensuring all keys and string values use double quotes, no trailing commas, and balanced brackets/braces.
Access `Property` values directly as attributes (e.g., `user.username`). If the property is indeed a list or dict (`ListProperty`, `DictProperty`), then subscripting is valid for its *value*, but not the `Property` object itself.
No dependency data recorded yet.