There is a JSON Schema that defines a JSON schema in JSON. However, it is troublesome to write JSON Schema by hand, so there is a tool for creating it. Among them, I will briefly explain a package called jsl that created a JSON Schema DSL in Python.
Use the following JSON Schema as a sample.
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
If this is defined using jsl, it will be as follows.
import jsl
class Example(jsl.Document):
class Options(object):
title = "Example Schema"
firstName = jsl.StringField(required=True)
lastName = jsl.StringField(required=True)
age = jsl.IntField(description="Age in years", minimum=0)
If you want to output this, use the class method get_schema.
import json
print(json.dumps(Example.get_schema(ordered=True), indent=4))
Recommended Posts