marshmallow-jsonschema: JSON Schema Generation from Marshmallow Schemas

0.13.0 · maintenance · verified Sun Apr 12

marshmallow-jsonschema translates marshmallow schemas into JSON Schema Draft v7 compliant jsonschema. It enables developers to generate JSON Schemas directly from their existing Marshmallow schemas, which is particularly useful for frontend form generation, API documentation, or validation in other systems. The current version is 0.13.0, released in October 2021, suggesting a maintenance rather than a rapid release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic Marshmallow schema and then use `marshmallow-jsonschema` to convert it into a JSON Schema representation. The output is a standard JSON Schema dictionary, ready for use in other applications.

from marshmallow import Schema, fields
from marshmallow_jsonschema import JSONSchema

class UserSchema(Schema):
    username = fields.String(required=True, metadata={'description': 'The user\'s unique identifier'})
    age = fields.Integer(required=False, allow_none=True, metadata={'minimum': 0})
    email = fields.Email(required=True)

# Instantiate the JSONSchema converter
json_schema_converter = JSONSchema()

# Convert your Marshmallow schema to a JSON Schema dictionary
user_json_schema = json_schema_converter.dump(UserSchema())

import json
print(json.dumps(user_json_schema, indent=2))

view raw JSON →