{"id":13936265,"url":"https://github.com/jazzband/jsonmodels","last_synced_at":"2025-05-14T23:06:28.079Z","repository":{"id":15106699,"uuid":"17833540","full_name":"jazzband/jsonmodels","owner":"jazzband","description":"jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.","archived":false,"fork":false,"pushed_at":"2025-04-07T17:29:55.000Z","size":389,"stargazers_count":340,"open_issues_count":47,"forks_count":51,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-08T00:02:29.013Z","etag":null,"topics":["json","jsonschema","models","python"],"latest_commit_sha":null,"homepage":"http://jsonmodels.readthedocs.org/en/latest/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jazzband.png","metadata":{"funding":{"custom":["https://jazzband.co/donate"]},"files":{"readme":"README.rst","changelog":"HISTORY.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2014-03-17T15:46:21.000Z","updated_at":"2025-04-04T04:15:47.000Z","dependencies_parsed_at":"2024-09-30T05:01:02.152Z","dependency_job_id":"219e5cda-ebdc-44f9-afa2-a3a32a98a80b","html_url":"https://github.com/jazzband/jsonmodels","commit_stats":{"total_commits":270,"total_committers":16,"mean_commits":16.875,"dds":0.2925925925925926,"last_synced_commit":"0c11e36ef95a012a60e13098c6149df5e9e291a1"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazzband%2Fjsonmodels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazzband%2Fjsonmodels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazzband%2Fjsonmodels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jazzband%2Fjsonmodels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jazzband","download_url":"https://codeload.github.com/jazzband/jsonmodels/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254243360,"owners_count":22038046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json","jsonschema","models","python"],"created_at":"2024-08-07T23:02:31.840Z","updated_at":"2025-05-14T23:06:23.066Z","avatar_url":"https://github.com/jazzband.png","language":"Python","readme":"===========\nJSON models\n===========\n\n.. image:: https://jazzband.co/static/img/badge.svg\n   :target: https://jazzband.co/\n   :alt: Jazzband\n\n.. image:: https://badge.fury.io/py/jsonmodels.svg\n   :target: http://badge.fury.io/py/jsonmodels\n\n.. image:: https://github.com/jazzband/jsonmodels/workflows/Test/badge.svg\n   :target: https://github.com/jazzband/jsonmodels/actions\n   :alt: Tests\n\n.. image:: https://img.shields.io/pypi/dm/jsonmodels.svg\n   :target: https://pypi.python.org/pypi/jsonmodels\n   :alt: PyPI\n\n.. image:: https://codecov.io/gh/jazzband/jsonmodels/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/jazzband/jsonmodels\n   :alt: Coverage\n\n`jsonmodels` is library to make it easier for you to deal with structures that\nare converted to, or read from JSON.\n\n* Free software: BSD license\n* Documentation: http://jsonmodels.rtfd.org\n* Source: https://github.com/jazzband/jsonmodels\n\nFeatures\n--------\n\n* Fully tested with Python 3.8+.\n\n* Support for PyPy 3.9 and 3.10 (see implementation notes in docs for more details).\n\n* Create Django-like models:\n\n  .. code-block:: python\n\n    from jsonmodels import models, fields, errors, validators\n\n\n    class Cat(models.Base):\n\n        name = fields.StringField(required=True)\n        breed = fields.StringField()\n        love_humans = fields.IntField(nullable=True)\n\n\n    class Dog(models.Base):\n\n        name = fields.StringField(required=True)\n        age = fields.IntField()\n\n\n    class Car(models.Base):\n\n        registration_number = fields.StringField(required=True)\n        engine_capacity = fields.FloatField()\n        color = fields.StringField()\n\n\n    class Person(models.Base):\n\n        name = fields.StringField(required=True)\n        surname = fields.StringField(required=True)\n        nickname = fields.StringField(nullable=True)\n        car = fields.EmbeddedField(Car)\n        pets = fields.ListField([Cat, Dog], nullable=True)\n\n* Access to values through attributes:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e cat = Cat()\n    \u003e\u003e\u003e cat.populate(name='Garfield')\n    \u003e\u003e\u003e cat.name\n    'Garfield'\n    \u003e\u003e\u003e cat.breed = 'mongrel'\n    \u003e\u003e\u003e cat.breed\n    'mongrel'\n\n* Validate models:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e person = Person(name='Chuck', surname='Norris')\n    \u003e\u003e\u003e person.validate()\n    None\n\n    \u003e\u003e\u003e dog = Dog()\n    \u003e\u003e\u003e dog.validate()\n    *** ValidationError: Field \"name\" is required!\n\n* Cast models to python struct and JSON:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e cat = Cat(name='Garfield')\n    \u003e\u003e\u003e dog = Dog(name='Dogmeat', age=9)\n    \u003e\u003e\u003e car = Car(registration_number='ASDF 777', color='red')\n    \u003e\u003e\u003e person = Person(name='Johny', surname='Bravo', pets=[cat, dog])\n    \u003e\u003e\u003e person.car = car\n    \u003e\u003e\u003e person.to_struct()\n    {\n        'car': {\n            'color': 'red',\n            'registration_number': 'ASDF 777'\n        },\n        'surname': 'Bravo',\n        'name': 'Johny',\n        'nickname': None,\n        'pets': [\n            {'name': 'Garfield'},\n            {'age': 9, 'name': 'Dogmeat'}\n        ]\n    }\n\n    \u003e\u003e\u003e import json\n    \u003e\u003e\u003e person_json = json.dumps(person.to_struct())\n\n* You don't like to write JSON Schema? Let `jsonmodels` do it for you:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e person = Person()\n    \u003e\u003e\u003e person.to_json_schema()\n    {\n        'additionalProperties': False,\n        'required': ['surname', 'name'],\n        'type': 'object',\n        'properties': {\n            'car': {\n                'additionalProperties': False,\n                'required': ['registration_number'],\n                'type': 'object',\n                'properties': {\n                    'color': {'type': 'string'},\n                    'engine_capacity': {'type': ''},\n                    'registration_number': {'type': 'string'}\n                }\n            },\n            'surname': {'type': 'string'},\n            'name': {'type': 'string'},\n            'nickname': {'type': ['string', 'null']}\n            'pets': {\n                'items': {\n                    'oneOf': [\n                        {\n                            'additionalProperties': False,\n                            'required': ['name'],\n                            'type': 'object',\n                            'properties': {\n                                'breed': {'type': 'string'},\n                                'name': {'type': 'string'}\n                            }\n                        },\n                        {\n                            'additionalProperties': False,\n                            'required': ['name'],\n                            'type': 'object',\n                            'properties': {\n                                'age': {'type': 'number'},\n                                'name': {'type': 'string'}\n                            }\n                        },\n                        {\n                            'type': 'null'\n                        }\n                    ]\n                },\n                'type': 'array'\n            }\n        }\n    }\n\n* Validate models and use validators, that affect generated schema:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e class Person(models.Base):\n    ...\n    ...     name = fields.StringField(\n    ...         required=True,\n    ...         validators=[\n    ...             validators.Regex('^[A-Za-z]+$'),\n    ...             validators.Length(3, 25),\n    ...         ],\n    ...     )\n    ...     age = fields.IntField(\n    ...         nullable=True,\n    ...         validators=[\n    ...             validators.Min(18),\n    ...             validators.Max(101),\n    ...         ]\n    ...     )\n    ...     nickname = fields.StringField(\n    ...         required=True,\n    ...         nullable=True\n    ...     )\n    ...\n\n    \u003e\u003e\u003e person = Person()\n    \u003e\u003e\u003e person.age = 11\n    \u003e\u003e\u003e person.validate()\n    *** ValidationError: '11' is lower than minimum ('18').\n    \u003e\u003e\u003e person.age = None\n    \u003e\u003e\u003e person.validate()\n    None\n\n    \u003e\u003e\u003e person.age = 19\n    \u003e\u003e\u003e person.name = 'Scott_'\n    \u003e\u003e\u003e person.validate()\n    *** ValidationError: Value \"Scott_\" did not match pattern \"^[A-Za-z]+$\".\n\n    \u003e\u003e\u003e person.name = 'Scott'\n    \u003e\u003e\u003e person.validate()\n    None\n\n    \u003e\u003e\u003e person.nickname = None\n    \u003e\u003e\u003e person.validate()\n    *** ValidationError: Field is required!\n\n    \u003e\u003e\u003e person.to_json_schema()\n    {\n        \"additionalProperties\": false,\n        \"properties\": {\n            \"age\": {\n                \"maximum\": 101,\n                \"minimum\": 18,\n                \"type\": [\"number\", \"null\"]\n            },\n            \"name\": {\n                \"maxLength\": 25,\n                \"minLength\": 3,\n                \"pattern\": \"/^[A-Za-z]+$/\",\n                \"type\": \"string\"\n            },\n            \"nickname\": {,\n                \"type\": [\"string\", \"null\"]\n            }\n        },\n        \"required\": [\n            \"nickname\",\n            \"name\"\n        ],\n        \"type\": \"object\"\n    }\n\n  You can also validate scalars, when needed:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e class Person(models.Base):\n    ...\n    ...     name = fields.StringField(\n    ...         required=True,\n    ...         validators=[\n    ...             validators.Regex('^[A-Za-z]+$'),\n    ...             validators.Length(3, 25),\n    ...         ],\n    ...     )\n    ...     age = fields.IntField(\n    ...         nullable=True,\n    ...         validators=[\n    ...             validators.Min(18),\n    ...             validators.Max(101),\n    ...         ]\n    ...     )\n    ...     nickname = fields.StringField(\n    ...         required=True,\n    ...         nullable=True\n    ...     )\n    ...\n\n    \u003e\u003e\u003e def only_odd_numbers(item):\n    ... if item % 2 != 1:\n    ...    raise validators.ValidationError(\"Only odd numbers are accepted\")\n    ...\n    \u003e\u003e\u003e class Person(models.Base):\n    ... lucky_numbers = fields.ListField(int, item_validators=[only_odd_numbers])\n    ... item_validator_str = fields.ListField(\n    ...        str,\n    ...        item_validators=[validators.Length(10, 20), validators.Regex(r\"\\w+\")],\n    ...        validators=[validators.Length(1, 2)],\n    ...    )\n    ...\n    \u003e\u003e\u003e Person.to_json_schema()\n    {\n        \"type\": \"object\",\n        \"additionalProperties\": false,\n        \"properties\": {\n            \"item_validator_str\": {\n                \"type\": \"array\",\n                \"items\": {\n                    \"type\": \"string\",\n                    \"minLength\": 10,\n                    \"maxLength\": 20,\n                    \"pattern\": \"/\\\\w+/\"\n                },\n                \"minItems\": 1,\n                \"maxItems\": 2\n            },\n            \"lucky_numbers\": {\n                \"type\": \"array\",\n                \"items\": {\n                    \"type\": \"number\"\n                }\n            }\n        }\n    }\n\n(Note that `only_odd_numbers` did not modify schema, since only class based validators are\nable to do that, though it will still work as expected in python. Use class based validators\nthat can be expressed in json schema if you want to be 100% correct on schema side.)\n\n* Lazy loading, best for circular references:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e class Primary(models.Base):\n    ...\n    ...     name = fields.StringField()\n    ...     secondary = fields.EmbeddedField('Secondary')\n\n    \u003e\u003e\u003e class Secondary(models.Base):\n    ...\n    ...    data = fields.IntField()\n    ...    first = fields.EmbeddedField('Primary')\n\n  You can use either `Model`, full path `path.to.Model` or relative imports\n  `.Model` or `...Model`.\n\n* Using definitions to generate schema for circular references:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e class File(models.Base):\n    ...\n    ...     name = fields.StringField()\n    ...     size = fields.FloatField()\n\n    \u003e\u003e\u003e class Directory(models.Base):\n    ...\n    ...     name = fields.StringField()\n    ...     children = fields.ListField(['Directory', File])\n\n    \u003e\u003e\u003e class Filesystem(models.Base):\n    ...\n    ...     name = fields.StringField()\n    ...     children = fields.ListField([Directory, File])\n\n    \u003e\u003e\u003e Filesystem.to_json_schema()\n    {\n        \"type\": \"object\",\n        \"properties\": {\n            \"name\": {\"type\": \"string\"}\n            \"children\": {\n                \"items\": {\n                    \"oneOf\": [\n                        \"#/definitions/directory\",\n                        \"#/definitions/file\"\n                    ]\n                },\n                \"type\": \"array\"\n            }\n        },\n        \"additionalProperties\": false,\n        \"definitions\": {\n            \"directory\": {\n                \"additionalProperties\": false,\n                \"properties\": {\n                    \"children\": {\n                        \"items\": {\n                            \"oneOf\": [\n                                \"#/definitions/directory\",\n                                \"#/definitions/file\"\n                            ]\n                        },\n                        \"type\": \"array\"\n                    },\n                    \"name\": {\"type\": \"string\"}\n                },\n                \"type\": \"object\"\n            },\n            \"file\": {\n                \"additionalProperties\": false,\n                \"properties\": {\n                    \"name\": {\"type\": \"string\"},\n                    \"size\": {\"type\": \"number\"}\n                },\n                \"type\": \"object\"\n            }\n        }\n    }\n\n* Dealing with schemaless data\n\n(Plese note that using schemaless fields can cause your models to get out of control - especially if\nyou are the one responsible for data schema. On the other hand there is usually the case when incomming\ndata are with no schema defined and schemaless fields are the way to go.)\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e class Event(models.Base):\n    ...\n    ...     name = fields.StringField()\n    ...     size = fields.FloatField()\n    ...     extra = fields.DictField()\n\n    \u003e\u003e\u003e Event.to_json_schema()\n    {\n        \"type\": \"object\",\n        \"additionalProperties\": false,\n        \"properties\": {\n            \"extra\": {\n                \"type\": \"object\"\n            },\n            \"name\": {\n                \"type\": \"string\"\n            },\n            \"size\": {\n                \"type\": \"float\"\n            }\n        }\n    }\n\n`DictField` allow to pass any dict of values (`\"type\": \"object\"`), but note, that it will not make any validation\non values except for the dict type.\n\n* Compare JSON schemas:\n\n  .. code-block:: python\n\n    \u003e\u003e\u003e from jsonmodels.utils import compare_schemas\n    \u003e\u003e\u003e schema1 = {'type': 'object'}\n    \u003e\u003e\u003e schema2 = {'type': 'array'}\n    \u003e\u003e\u003e compare_schemas(schema1, schema1)\n    True\n    \u003e\u003e\u003e compare_schemas(schema1, schema2)\n    False\n\nMore\n----\n\nFor more examples and better description see full documentation:\nhttp://jsonmodels.rtfd.org.\n","funding_links":["https://jazzband.co/donate"],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjazzband%2Fjsonmodels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjazzband%2Fjsonmodels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjazzband%2Fjsonmodels/lists"}