{"id":13501788,"url":"https://github.com/fuhrysteve/marshmallow-jsonschema","last_synced_at":"2025-05-16T03:01:58.521Z","repository":{"id":2756084,"uuid":"47362774","full_name":"fuhrysteve/marshmallow-jsonschema","owner":"fuhrysteve","description":"JSON Schema Draft v7 (http://json-schema.org/) formatting with marshmallow","archived":false,"fork":false,"pushed_at":"2024-04-02T15:07:05.000Z","size":237,"stargazers_count":208,"open_issues_count":62,"forks_count":73,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-19T04:29:19.296Z","etag":null,"topics":["json-schema","marshmallow","marshmallow-jsonschema","react-jsonschema-form"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fuhrysteve.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-12-03T21:44:46.000Z","updated_at":"2024-12-30T22:22:23.000Z","dependencies_parsed_at":"2023-07-06T05:56:33.416Z","dependency_job_id":"3193b3f9-dedd-429f-9ca6-758bba328a87","html_url":"https://github.com/fuhrysteve/marshmallow-jsonschema","commit_stats":{"total_commits":209,"total_committers":39,"mean_commits":5.358974358974359,"dds":0.6794258373205742,"last_synced_commit":"7446d3dffec8656d37151fd577b1330e2209b370"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuhrysteve%2Fmarshmallow-jsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuhrysteve%2Fmarshmallow-jsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuhrysteve%2Fmarshmallow-jsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fuhrysteve%2Fmarshmallow-jsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fuhrysteve","download_url":"https://codeload.github.com/fuhrysteve/marshmallow-jsonschema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254459077,"owners_count":22074604,"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-schema","marshmallow","marshmallow-jsonschema","react-jsonschema-form"],"created_at":"2024-07-31T22:01:50.352Z","updated_at":"2025-05-16T03:01:58.431Z","avatar_url":"https://github.com/fuhrysteve.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"## marshmallow-jsonschema: JSON Schema formatting with marshmallow\n\n![Build Status](https://github.com/fuhrysteve/marshmallow-jsonschema/workflows/build/badge.svg)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/python/black)\n\n marshmallow-jsonschema translates marshmallow schemas into\n JSON Schema Draft v7 compliant jsonschema. See http://json-schema.org/\n\n#### Why would I want my schema translated to JSON?\n\nWhat are the use cases for this? Let's say you have a\nmarshmallow schema in python, but you want to render your\nschema as a form in another system (for example: a web browser\nor mobile device).\n\n#### Installation\n\nRequires python\u003e=3.6 and marshmallow\u003e=3.11. (For python 2 \u0026 marshmallow 2 support, please use marshmallow-jsonschema\u003c0.11)\n\n```\npip install marshmallow-jsonschema\n```\n\n#### Some Client tools can render forms using JSON Schema\n\n* [react-jsonschema-form](https://github.com/mozilla-services/react-jsonschema-form) (recommended)\n  * See below extension for this excellent library!\n* https://github.com/brutusin/json-forms\n* https://github.com/jdorn/json-editor\n* https://github.com/ulion/jsonform\n\n### Examples\n\n#### Simple Example\n\n```python\nfrom marshmallow import Schema, fields\nfrom marshmallow_jsonschema import JSONSchema\n\nclass UserSchema(Schema):\n    username = fields.String()\n    age = fields.Integer()\n    birthday = fields.Date()\n\nuser_schema = UserSchema()\n\njson_schema = JSONSchema()\njson_schema.dump(user_schema)\n```\n\nYields:\n\n```python\n{'properties': {'age': {'format': 'integer',\n                        'title': 'age',\n                        'type': 'number'},\n                'birthday': {'format': 'date',\n                             'title': 'birthday',\n                             'type': 'string'},\n                'username': {'title': 'username', 'type': 'string'}},\n 'required': [],\n 'type': 'object'}\n```\n\n#### Nested Example\n\n```python\nfrom marshmallow import Schema, fields\nfrom marshmallow_jsonschema import JSONSchema\nfrom tests import UserSchema\n\n\nclass Athlete(object):\n    user_schema = UserSchema()\n\n    def __init__(self):\n        self.name = 'sam'\n\n\nclass AthleteSchema(Schema):\n    user_schema = fields.Nested(JSONSchema)\n    name = fields.String()\n\n    \nathlete = Athlete()\nathlete_schema = AthleteSchema()\n\nathlete_schema.dump(athlete)\n```\n\n#### Complete example Flask application using brutisin/json-forms\n\n![Screenshot](http://i.imgur.com/jJv1wFk.png)\n\nThis example renders a form not dissimilar to how [wtforms](https://github.com/wtforms/wtforms) might render a form.\n\nHowever rather than rendering the form in python, the JSON Schema is rendered using the\njavascript library [brutusin/json-forms](https://github.com/brutusin/json-forms).\n\n\n```python\nfrom flask import Flask, jsonify\nfrom marshmallow import Schema, fields\nfrom marshmallow_jsonschema import JSONSchema\n\napp = Flask(__name__)\n\n\nclass UserSchema(Schema):\n    name = fields.String()\n    address = fields.String()\n\n\n@app.route('/schema')\ndef schema():\n    schema = UserSchema()\n    return jsonify(JSONSchema().dump(schema))\n\n\n@app.route('/')\ndef home():\n    return '''\u003c!DOCTYPE html\u003e\n\u003chead\u003e\n\u003clink rel=\"stylesheet\" href=\"https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/css/brutusin-json-forms.css\"\u003e\u003cPaste\u003e\n\u003cscript src=\"https://code.jquery.com/jquery-1.12.1.min.js\" integrity=\"sha256-I1nTg78tSrZev3kjvfdM5A5Ak/blglGzlaZANLPDl3I=\" crossorigin=\"anonymous\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.3.4/underscore.string.min.js\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/js/brutusin-json-forms.min.js\"\u003e\u003c/script\u003e\n\u003cscript\u003e\n$(document).ready(function() {\n    $.ajax({\n        url: '/schema'\n        , success: function(data) {\n            var container = document.getElementById('myform');\n            var BrutusinForms = brutusin[\"json-forms\"];\n            var bf = BrutusinForms.create(data);\n            bf.render(container);\n        }\n    });\n});\n\u003c/script\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n\u003cdiv id=\"myform\"\u003e\u003c/div\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n'''\n\n\nif __name__ == '__main__':\n    app.run(host='0.0.0.0', debug=True)\n\n```\n\n\n### Advanced usage\n#### Custom Type support\n\nSimply add a `_jsonschema_type_mapping` method to your field\nso we know how it ought to get serialized to JSON Schema.\n\nA common use case for this is creating a dropdown menu using\nenum (see Gender below).\n\n\n```python\nclass Colour(fields.Field):\n\n    def _jsonschema_type_mapping(self):\n        return {\n            'type': 'string',\n        }\n\n    def _serialize(self, value, attr, obj):\n        r, g, b = value\n        r = \"%02X\" % (r,)\n        g = \"%02X\" % (g,)\n        b = \"%02X\" % (b,)\n        return '#' + r + g + b \n\nclass Gender(fields.String):\n    def _jsonschema_type_mapping(self):\n        return {\n            'type': 'string',\n            'enum': ['Male', 'Female']\n        }\n\n\nclass UserSchema(Schema):\n    name = fields.String(required=True)\n    favourite_colour = Colour()\n    gender = Gender()\n\nschema = UserSchema()\njson_schema = JSONSchema()\njson_schema.dump(schema)\n```\n\n\n### React-JSONSchema-Form Extension\n\n[react-jsonschema-form](https://react-jsonschema-form.readthedocs.io/en/latest/)\nis a library for rendering jsonschemas as a form using React. It is very powerful\nand full featured.. the catch is that it requires a proprietary\n[`uiSchema`](https://react-jsonschema-form.readthedocs.io/en/latest/form-customization/#the-uischema-object)\nto provide advanced control how the form is rendered.\n[Here's a live playground](https://rjsf-team.github.io/react-jsonschema-form/)\n\n*(new in version 0.10.0)*\n\n```python\nfrom marshmallow_jsonschema.extensions import ReactJsonSchemaFormJSONSchema\n\nclass MySchema(Schema):\n    first_name = fields.String(\n        metadata={\n            'ui:autofocus': True,\n        }\n    )\n    last_name = fields.String()\n\n    class Meta:\n        react_uischema_extra = {\n            'ui:order': [\n                'first_name',\n                'last_name',\n            ]\n        }\n\n\njson_schema_obj = ReactJsonSchemaFormJSONSchema()\nschema = MySchema()\n\n# here's your jsonschema\ndata = json_schema_obj.dump(schema)\n\n# ..and here's your uiSchema!\nui_schema_json = json_schema_obj.dump_uischema(schema)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuhrysteve%2Fmarshmallow-jsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffuhrysteve%2Fmarshmallow-jsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffuhrysteve%2Fmarshmallow-jsonschema/lists"}