{"id":7327,"library":"jsonobject","title":"jsonobject","description":"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.","status":"active","version":"2.3.1","language":"en","source_language":"en","source_url":"https://github.com/dimagi/jsonobject","tags":["json","schema","serialization","deserialization","object-mapping","validation"],"install":[{"cmd":"pip install jsonobject","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"JsonObject","correct":"from jsonobject import JsonObject"},{"symbol":"StringProperty","correct":"from jsonobject import StringProperty"},{"symbol":"IntegerProperty","correct":"from jsonobject import IntegerProperty"},{"symbol":"BooleanProperty","correct":"from jsonobject import BooleanProperty"},{"symbol":"DateTimeProperty","correct":"from jsonobject import DateTimeProperty"},{"symbol":"ListProperty","correct":"from jsonobject import ListProperty"},{"symbol":"DictProperty","correct":"from jsonobject import DictProperty"}],"quickstart":{"code":"import datetime\nfrom jsonobject import JsonObject, StringProperty, BooleanProperty, DateTimeProperty, ListProperty\n\nclass User(JsonObject):\n    username = StringProperty(required=True)\n    name = StringProperty()\n    active = BooleanProperty(default=False)\n    date_joined = DateTimeProperty()\n    tags = ListProperty(str)\n\n# Create an object\nuser1 = User(\n    name='Jane Doe',\n    username='janedoe',\n    date_joined=datetime.datetime.utcnow(),\n    tags=['developer', 'python']\n)\n\nprint(f\"User object: {user1}\")\nprint(f\"User to JSON: {user1.to_json()}\")\n\n# Create from existing JSON\njson_data = {\n    'username': 'alice',\n    'name': 'Alice Smith',\n    'active': True,\n    'date_joined': '2023-01-15T10:30:00Z',\n    'tags': ['tester']\n}\nuser2 = User(json_data)\nprint(f\"User from JSON: {user2}\")\nprint(f\"Is user2 active? {user2.active}\")","lang":"python","description":"Define a `JsonObject` subclass with various `Property` types, instantiate objects, and serialize them to JSON. Also demonstrates deserialization from a dictionary."},"warnings":[{"fix":"Review code that relies on `ListProperty` raising `BadValueError` for iterable value types and adjust expectations for receiving a plain Python list instead.","message":"Version 2.0.0 changed the behavior of `ListProperty` when an iterable is passed as its value type. Previously, it might have raised `BadValueError`; now, it returns a plain Python list. This was considered a fix for unintuitive behavior but could be breaking if previous error handling was relied upon.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"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).","message":"Version 1.0.0 officially dropped support for Python 2.7, 3.5, and 3.6. Using `jsonobject` 1.0.0 or higher with these Python versions will result in compatibility errors.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always use a `lambda` or a callable for mutable default values, e.g., `ListProperty(default=list)` or `ListProperty(default=lambda: [])`.","message":"When defining `Property` types, be cautious with mutable default values (e.g., `ListProperty(default=[])` or `DictProperty(default={})`). If a mutable object is used as a default, all instances of the `JsonObject` class will share the *same* mutable object, leading to unexpected side effects when one instance modifies it.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"The `jsonobject` library provides schema-driven object mapping for JSON. It is distinct from Python's built-in `json` module, which offers basic JSON encoding/decoding, and also distinct from Java libraries often named `JSONObject`. Avoid confusing their APIs or expecting direct interoperability without explicit conversion.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure that your dictionary is correctly passed to the `JsonObject` constructor: `my_object = MyJsonObject(my_dict_data)`.","cause":"Attempting to access a property on a standard Python dictionary (`dict`) as if it were a `jsonobject.JsonObject` instance. This occurs when JSON data has been loaded using `json.loads()` or similar, but not wrapped/instantiated as a `JsonObject`.","error":"AttributeError: 'dict' object has no attribute 'some_property'"},{"fix":"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.","cause":"A value assigned to a `Property` or provided during `JsonObject` instantiation does not match the expected type defined in the schema (e.g., providing a string to an `IntegerProperty`).","error":"jsonobject.exceptions.BadValueError: Expected type <ExpectedType>, got <ActualType>"},{"fix":"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.","cause":"The input string provided to `JsonObject` constructor (or `json.loads` if you're manually parsing) is not valid JSON. This can be due to missing quotes, unescaped characters, trailing commas, or incorrect bracket/brace balancing.","error":"json.decoder.JSONDecodeError: Expecting value: line X column Y (char Z)"},{"fix":"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.","cause":"Attempting to access an attribute of a `JsonObject` (which is a `Property` object) using square brackets (`[]`) as if it were a dictionary or list, when it is not. For example, trying `user.username[0]` for a `StringProperty`.","error":"TypeError: 'PropertyName' object is not subscriptable"}]}