Skip to main content
Warning: You are using the test version of PyPI. This is a pre-production deployment of Warehouse. Changes made here affect the production instance of TestPyPI (
Help us improve Python packaging - Donate today!

JSON Schema Draft v4 ( formatting with marshmallow

Project Description

marshmallow-jsonschema translates marshmallow schemas into JSON Schema Draft v4 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).


pip install marshmallow-jsonschema

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

[Screenshot] (

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

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)

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 = hex(r)[2:]
        g = hex(g)[2:]
        b = hex(b)[2:]
        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()

Release History

This version
History Node


History Node


History Node


History Node


Download Files

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

File Name & Hash SHA256 Hash Help Version File Type Upload Date
(6.6 kB) Copy SHA256 Hash SHA256
Source Jul 12, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting