Package: marshmallow-jsonschema
marshmallow-jsonschema: JSON Schema formatting with marshmallowmarshmallow-jsonschema translates marshmallow schemas into
JSON Schema Draft v7 compliant jsonschema. Seehttp://json-schema.org/Why would I want my schema translated to JSON?What are the use cases for this? Let's say you have a
marshmallow schema in python, but you want to render your
schema as a form in another system (for example: a web browser
or mobile device).InstallationRequires python>=3.6 and marshmallow>=3.11. (For python 2 & marshmallow 2 support, please use marshmallow-jsonschema<0.11)pip install marshmallow-jsonschemaSome Client tools can render forms using JSON Schemareact-jsonschema-form(recommended)See below extension for this excellent library!https://github.com/brutusin/json-formshttps://github.com/jdorn/json-editorhttps://github.com/ulion/jsonformExamplesSimple ExamplefrommarshmallowimportSchema,fieldsfrommarshmallow_jsonschemaimportJSONSchemaclassUserSchema(Schema):username=fields.String()age=fields.Integer()birthday=fields.Date()user_schema=UserSchema()json_schema=JSONSchema()json_schema.dump(user_schema)Yields:{'properties':{'age':{'format':'integer','title':'age','type':'number'},'birthday':{'format':'date','title':'birthday','type':'string'},'username':{'title':'username','type':'string'}},'required':[],'type':'object'}Nested ExamplefrommarshmallowimportSchema,fieldsfrommarshmallow_jsonschemaimportJSONSchemafromtestsimportUserSchemaclassAthlete(object):user_schema=UserSchema()def__init__(self):self.name='sam'classAthleteSchema(Schema):user_schema=fields.Nested(JSONSchema)name=fields.String()athlete=Athlete()athlete_schema=AthleteSchema()athlete_schema.dump(athlete)Complete example Flask application using brutisin/json-formsThis example renders a form not dissimilar to howwtformsmight render a form.However rather than rendering the form in python, the JSON Schema is rendered using the
javascript librarybrutusin/json-forms.fromflaskimportFlask,jsonifyfrommarshmallowimportSchema,fieldsfrommarshmallow_jsonschemaimportJSONSchemaapp=Flask(__name__)classUserSchema(Schema):name=fields.String()address=fields.String()@app.route('/schema')defschema():schema=UserSchema()returnjsonify(JSONSchema().dump(schema))@app.route('/')defhome():return'''<!DOCTYPE html><head><link rel="stylesheet" href="https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/css/brutusin-json-forms.css"><Paste><script src="https://code.jquery.com/jquery-1.12.1.min.js" integrity="sha256-I1nTg78tSrZev3kjvfdM5A5Ak/blglGzlaZANLPDl3I=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.string/3.3.4/underscore.string.min.js"></script><script src="https://cdn.jsdelivr.net/brutusin.json-forms/1.3.0/js/brutusin-json-forms.min.js"></script><script>$(document).ready(function() {$.ajax({url: '/schema', success: function(data) {var container = document.getElementById('myform');var BrutusinForms = brutusin["json-forms"];var bf = BrutusinForms.create(data);bf.render(container);}});});</script></head><body><div id="myform"></div></body></html>'''if__name__=='__main__':app.run(host='0.0.0.0',debug=True)Advanced usageCustom Type supportSimply add a_jsonschema_type_mappingmethod to your field
so we know how it ought to get serialized to JSON Schema.A common use case for this is creating a dropdown menu using
enum (see Gender below).classColour(fields.Field):def_jsonschema_type_mapping(self):return{'type':'string',}def_serialize(self,value,attr,obj):r,g,b=valuer="%02X"%(r,)g="%02X"%(g,)b="%02X"%(b,)return'#'+r+g+bclassGender(fields.String):def_jsonschema_type_mapping(self):return{'type':'string','enum':['Male','Female']}classUserSchema(Schema):name=fields.String(required=True)favourite_colour=Colour()gender=Gender()schema=UserSchema()json_schema=JSONSchema()json_schema.dump(schema)React-JSONSchema-Form Extensionreact-jsonschema-formis a library for rendering jsonschemas as a form using React. It is very powerful
and full featured.. the catch is that it requires a proprietaryuiSchemato provide advanced control how the form is rendered.Here's a live playground(new in version 0.10.0)frommarshmallow_jsonschema.extensionsimportReactJsonSchemaFormJSONSchemaclassMySchema(Schema):first_name=fields.String(metadata={'ui:autofocus':True,})last_name=fields.String()classMeta:react_uischema_extra={'ui:order':['first_name','last_name',]}json_schema_obj=ReactJsonSchemaFormJSONSchema()schema=MySchema()# here's your jsonschemadata=json_schema_obj.dump(schema)# ..and here's your uiSchema!ui_schema_json=json_schema_obj.dump_uischema(schema)