Package: alchemyjsonschema
featuresalchemyjsonschema is the library for converting sqlalchemys’s model to jsonschema.using alchemyjsonschema as commandusing alchemyjsonschema as libraryas libraryhaving three output styles.NoForeignKeyWalker – ignore relationshipsForeignKeyWalker – expecting the information about relationship is foreign keyStructuralWalker – fullset output(expecting the information about relationship is full JSON data)examplesdumping json with above three output styles.target models are here. Group and User.# -*- coding:utf-8 -*-importsqlalchemyassaimportsqlalchemy.ormasormfromsqlalchemy.ext.declarativeimportdeclarative_baseBase=declarative_base()classGroup(Base):"""model for test"""__tablename__="Group"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=False)classUser(Base):__tablename__="User"pk=sa.Column(sa.Integer,primary_key=True,doc="primary key")name=sa.Column(sa.String(255),default="",nullable=True)group_id=sa.Column(sa.Integer,sa.ForeignKey(Group.pk),nullable=False)group=orm.relationship(Group,uselist=False,backref="users")NoForeignKeyWalkerimportpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportNoForeignKeyWalkerfactory=SchemaFactory(NoForeignKeyWalker)pp.pprint(factory(User))"""
{'properties': {'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""ForeignKeyWalkerimportpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportForeignKeyWalkerfactory=SchemaFactory(ForeignKeyWalker)pp.pprint(factory(User))"""
{'properties': {'group_id': {'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'pk': {'description': 'primary key', 'type': 'integer'}},
 'required': ['pk', 'group_id'],
 'title': 'User',
 'type': 'object'}
"""StructuralWalkerimportpprintasppfromalchemyjsonschemaimportSchemaFactoryfromalchemyjsonschemaimportStructuralWalkerfactory=SchemaFactory(StructuralWalker)pp.pprint(factory(User))"""
{'definitions': {'Group': {'properties': {'pk': {'description': 'primary key',
                                                 'type': 'integer'},
                                          'name': {'maxLength': 255,
                                                   'type': 'string'}},
                           'type': 'object'}},
 'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'group': {'$ref': '#/definitions/Group'}},
 'required': ['pk'],
 'title': 'User',
 'type': 'object'}
"""pp.pprint(factory(Group))"""
{'definitions': {'User': {'properties': {'pk': {'description': 'primary key',
                                                'type': 'integer'},
                                         'name': {'maxLength': 255,
                                                  'type': 'string'}},
                          'type': 'object'}},
 'description': 'model for test',
 'properties': {'pk': {'description': 'primary key', 'type': 'integer'},
                'name': {'maxLength': 255, 'type': 'string'},
                'users': {'items': {'$ref': '#/definitions/User'},
                          'type': 'array'}},
 'required': ['pk', 'name'],
 'title': 'Group',
 'type': 'object'}
"""as commandusing alchemyjsonschema as command (the command name is alsoalchemyjsonschema).help$alchemyjsonschema--helpusage:alchemyjsonschema[-h][--walker{noforeignkey,foreignkey,structural}][--decision{default,fullset}][--depthDEPTH][--outOUT]targetpositionalarguments:targetthemoduleorclasstoextractschemasfromoptionalarguments:-h,--helpshowthishelpmessageandexit--walker{noforeignkey,foreignkey,structural}--decision{default,fullset}--depthDEPTH--outOUToutputtofileIf above two model definitions (User,Group) are existed inalchemyjsonschema.tests.models.Target is the class position or module position. for example,class position –alchemyjsonschema.tests.models:Usermodule position –alchemyjsonschema.tests.modelsexampleUsing StructuralWalker via command line (–walker structural).
Of course, NoForeignKeyWalker is noforeignkey, and ForeignKeyWalker is foreignkey.$alchemyjsonschema--walkerstructuralalchemyjsonschema.tests.models:Group{"definitions":{"Group":{"properties":{"color":{"enum":["red","green","yellow","blue"],"maxLength":6,"type":"string"},"created_at":{"format":"date-time","type":"string"},"name":{"maxLength":255,"type":"string"},"pk":{"description":"primary key","type":"integer"},"users":{"items":{"$ref":"#/definitions/User"},"type":"array"}},"required":["pk"],"title":"Group","type":"object"},"User":{"properties":{"created_at":{"format":"date-time","type":"string"},"name":{"maxLength":255,"type":"string"},"pk":{"description":"primary key","type":"integer"}},"required":["pk"],"type":"object"}}}Output is not same when using Walker-class, directly. This is handy output for something like a swagger(OpenAPI 2.0)’s tool.appendix: what is–decision?what is–decision? (TODO: gentle description)$alchemyjsonschema--walkerstructuralalchemyjsonschema.tests.models:User|jq.-S>/tmp/default.json$alchemyjsonschema--decisionuseforeignkey--walkerstructuralalchemyjsonschema.tests.models:User|jq.-S>/tmp/useforeignkey.json$diff-u/tmp/default.json/tmp/useforeignkey.json--- /tmp/default.json 2017-01-02 22:49:44.000000000 +0900+++ /tmp/useforeignkey.json   2017-01-02 22:53:13.000000000 +0900@@ -1,43 +1,14 @@{"definitions": {-    "Group": {-      "properties": {-        "color": {-          "enum": [-            "red",-            "green",-            "yellow",-            "blue"-          ],-          "maxLength": 6,-          "type": "string"-        },-        "created_at": {-          "format": "date-time",-          "type": "string"-        },-        "name": {-          "maxLength": 255,-          "type": "string"-        },-        "pk": {-          "description": "primary key",-          "type": "integer"-        }-      },-      "required": [-        "pk"-      ],-      "type": "object"-    },"User": {"properties": {"created_at": {"format": "date-time","type": "string"},-        "group": {-          "$ref": "#/definitions/Group"+        "group_id": {+          "relation": "group",+          "type": "integer"},"name": {"maxLength": 255,