{"id":14839,"library":"python-jsonschema-objects","title":"Python JSONSchema Objects","description":"python-jsonschema-objects is an active Python library (version 0.5.7) that provides automatic class-based binding to JSON Schemas, generating Python objects with built-in validation. It aims to reduce the tedium of writing object bindings for JSON schemas by auto-generating classes that seamlessly encode to and from JSON, complete with validations. The library has a moderate release cadence, with the last release in November 2024.","status":"active","version":"0.5.7","language":"en","source_language":"en","source_url":"https://github.com/cwacek/python-jsonschema-objects","tags":["JSON Schema","validation","codegen","data binding"],"install":[{"cmd":"pip install python-jsonschema-objects","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used internally for schema resolution and validation, specifically `jsonschema.RefResolver` and `jsonschema.exceptions.ValidationError`.","package":"jsonschema"}],"imports":[{"note":"The primary interface is `ObjectBuilder`, typically accessed via the aliased package import `python_jsonschema_objects as pjs`.","wrong":"from python_jsonschema_objects import ObjectBuilder","symbol":"ObjectBuilder","correct":"import python_jsonschema_objects as pjs\nbuilder = pjs.ObjectBuilder(schema)"}],"quickstart":{"code":"import python_jsonschema_objects as pjs\nimport json\n\nschema_str = '''\n{\n  \"title\": \"Example Schema\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"firstName\": {\"type\": \"string\"},\n    \"lastName\": {\"type\": \"string\"},\n    \"age\": {\"description\": \"Age in years\", \"type\": \"integer\", \"minimum\": 0},\n    \"dogs\": {\"type\": \"array\", \"items\": {\"type\": \"string\"}, \"maxItems\": 4}\n  },\n  \"required\": [\"firstName\", \"lastName\"]\n}\n'''\nschema = json.loads(schema_str)\n\nbuilder = pjs.ObjectBuilder(schema)\nns = builder.build_classes()\n\n# Access the generated class by its title\nPerson = ns.ExampleSchema\n\n# Create an instance and set properties\njames = Person(firstName=\"James\", lastName=\"Bond\")\nprint(f\"Created person: {james.firstName} {james.lastName}\")\n\n# Properties are validated on assignment\ntry:\n    james.age = -2\nexcept pjs.ValidationError as e:\n    print(f\"Validation error: {e.message}\")\n\ntry:\n    james.dogs = [\"Jasper\", \"Spot\", \"Noodles\", \"Fido\", \"Dumbo\"]\nexcept pjs.ValidationError as e:\n    print(f\"Validation error: {e.message}\")\n\n# Serialize the object to JSON\njson_output = james.serialize(sort_keys=True)\nprint(f\"Serialized object: {json_output}\")","lang":"python","description":"This quickstart demonstrates how to define a JSON Schema, build Python classes from it using `ObjectBuilder`, instantiate a generated class, and observe built-in validation upon property assignment. It also shows how to serialize the object back to JSON."},"warnings":[{"fix":"Monitor `python-jsonschema-objects` updates for `referencing` library adoption. If issues arise, consider pinning `jsonschema < 4.0.0` in your project's dependencies temporarily, or explicitly handle `$ref` resolution if possible.","message":"The `jsonschema` library, a core dependency, has deprecated `jsonschema.RefResolver` in its v4.0.0+ releases in favor of the `referencing` library. `python-jsonschema-objects` still relies on `jsonschema.RefResolver`. This might lead to compatibility issues or unexpected behavior if a very recent `jsonschema` version is installed alongside `python-jsonschema-objects` or if `RefResolver`'s behavior changes significantly in future `jsonschema` updates.","severity":"gotcha","affected_versions":"All versions depending on `jsonschema < 4.0.0` or `jsonschema >= 4.0.0` where `RefResolver` is deprecated."},{"fix":"Always anchor regular expression patterns in your JSON schemas with `^` at the beginning and `$` at the end (e.g., `\"pattern\": \"^[Y|N]$\"`) to ensure exact matches and portability.","message":"Regular expressions in JSON Schema (and thus in `python-jsonschema-objects`) are interpreted using Python's full regex engine, not just the subset allowed by the JSON Schema specification. This can lead to non-portable schemas or unexpected matches if patterns are not properly anchored with `^` (start) and `$` (end).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Add `\"additionalProperties\": false` to any object schemas where you want to strictly control the allowed properties.","message":"By default, JSON Schemas often allow additional properties beyond those explicitly defined. If strictness is desired (i.e., disallowing any properties not defined in the schema), you must explicitly set `\"additionalProperties\": false` in your schema. Otherwise, `python-jsonschema-objects` will allow extra fields when deserializing or validating objects.","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 assigned values adhere to all schema constraints, such as `minimum`, `maximum`, `minLength`, `maxLength`, etc. Example: `james.age = 5` instead of `james.age = -2`.","cause":"Attempting to assign a value that violates a `minimum` constraint defined in the JSON Schema.","error":"ValidationError: -2 is less than 0"},{"fix":"Provide an array with a number of elements within the bounds specified by `minItems` and `maxItems`. Example: `james.dogs = ['Jasper', 'Spot']`.","cause":"Attempting to assign an array with more items than allowed by the `maxItems` constraint defined in the JSON Schema.","error":"ValidationError: ['Jasper', 'Spot', 'Noodles', 'Fido', 'Dumbo'] has too many elements. Wanted 4."},{"fix":"Ensure your schema has a `\"title\"` property (e.g., `\"title\": \"MySchema\"`) and access the generated class using `ns.MySchema`. Alternatively, iterate through `ns.__dict__` to see available generated classes.","cause":"The generated class for your schema could not be found in the `Namespace` object returned by `builder.build_classes()`. This often happens if the schema's `title` field doesn't match the expected attribute name, or if `named_only=True` was used without a `title`.","error":"AttributeError: 'Namespace' object has no attribute 'MySchema'"}],"ecosystem":"pypi"}