Skip to main content

JSON Schema Draft v7 ( formatting with marshmallow

Project description

marshmallow-jsonschema: JSON Schema formatting with marshmallow

Build Status Coverage Status Code style: black

marshmallow-jsonschema translates marshmallow schemas into JSON Schema Draft v7 compliant jsonschema. See

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).


Requires python>=3.6 and marshmallow>=3.11. (For python 2 & marshmallow 2 support, please use marshmallow-jsonschema<0.11)

pip install marshmallow-jsonschema

Some Client tools can render forms using JSON Schema


Simple Example

from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema

class UserSchema(Schema):
    username = fields.String()
    age = fields.Integer()
    birthday = fields.Date()

user_schema = UserSchema()

json_schema = JSONSchema()


{'properties': {'age': {'format': 'integer',
                        'title': 'age',
                        'type': 'number'},
                'birthday': {'format': 'date',
                             'title': 'birthday',
                             'type': 'string'},
                'username': {'title': 'username', 'type': 'string'}},
 'required': [],
 'type': 'object'}

Nested Example

from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema
from tests import UserSchema

class Athlete(object):
    user_schema = UserSchema()

    def __init__(self): = 'sam'

class AthleteSchema(Schema):
    user_schema = fields.Nested(JSONSchema)
    name = fields.String()

athlete = Athlete()
athlete_schema = AthleteSchema()


Complete example Flask application using brutisin/json-forms


This example renders a form not dissimilar to how wtforms might render a form.

However rather than rendering the form in python, the JSON Schema is rendered using the javascript library brutusin/json-forms.

from flask import Flask, jsonify
from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema

app = Flask(__name__)

class UserSchema(Schema):
    name = fields.String()
    address = fields.String()

def schema():
    schema = UserSchema()
    return jsonify(JSONSchema().dump(schema))

def home():
    return '''<!DOCTYPE html>
<link rel="stylesheet" href=""><Paste>
<script src="" integrity="sha256-I1nTg78tSrZev3kjvfdM5A5Ak/blglGzlaZANLPDl3I=" crossorigin="anonymous"></script>
<script src=""></script>
<script src=""></script>
$(document).ready(function() {
        url: '/schema'
        , success: function(data) {
            var container = document.getElementById('myform');
            var BrutusinForms = brutusin["json-forms"];
            var bf = BrutusinForms.create(data);
<div id="myform"></div>

if __name__ == '__main__':'', debug=True)

Advanced usage

Custom Type support

Simply add a _jsonschema_type_mapping method 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).

class Colour(fields.Field):

    def _jsonschema_type_mapping(self):
        return {
            'type': 'string',

    def _serialize(self, value, attr, obj):
        r, g, b = value
        r = "%02X" % (r,)
        g = "%02X" % (g,)
        b = "%02X" % (b,)
        return '#' + r + g + b 

class Gender(fields.String):
    def _jsonschema_type_mapping(self):
        return {
            'type': 'string',
            'enum': ['Male', 'Female']

class UserSchema(Schema):
    name = fields.String(required=True)
    favourite_colour = Colour()
    gender = Gender()

schema = UserSchema()
json_schema = JSONSchema()

React-JSONSchema-Form Extension

react-jsonschema-form is a library for rendering jsonschemas as a form using React. It is very powerful and full featured.. the catch is that it requires a proprietary uiSchema to provide advanced control how the form is rendered. Here's a live playground

(new in version 0.10.0)

from marshmallow_jsonschema.extensions import ReactJsonSchemaFormJSONSchema

class MySchema(Schema):
    first_name = fields.String(
            'ui:autofocus': True,
    last_name = fields.String()

    class Meta:
        react_uischema_extra = {
            'ui:order': [

json_schema_obj = ReactJsonSchemaFormJSONSchema()
schema = MySchema()

# here's your jsonschema
data = json_schema_obj.dump(schema)

# ..and here's your uiSchema!
ui_schema_json = json_schema_obj.dump_uischema(schema)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for marshmallow-jsonschema, version 0.13.0
Filename, size File type Python version Upload date Hashes
Filename, size marshmallow_jsonschema-0.13.0-py3-none-any.whl (11.8 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size marshmallow-jsonschema-0.13.0.tar.gz (12.7 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page