JSON Schema Generator#

The JSON Schema generator produces a JSON Schema definition based on a given B-UML model. This schema can then be used to validate JSON objects against the model’s structure and constraints.

Let’s generate the JSON Schema for our Structural model example. You should create a JSONSchemaGenerator object, provide the Structural model, and use the generate method as follows:

from besser.generators.json import JSONSchemaGenerator

generator: JSONSchemaGenerator = JSONSchemaGenerator(model=library_model)
generator.generate()

The json_schema.json file containing the JSON Schema will be generated in the <<current_directory>>/output folder and it will look as follows.

  1{
  2    "$schema": "http://json-schema.org/draft-07/schema#",
  3    "title": "Generated JSON Schema",
  4    "type": "object",
  5    "properties": {
  6        "Book": {
  7            "allOf": [{
  8                "type": "object",
  9                "properties": {
 10                    "pages": {
 11                        "type": "integer"
 12                    },
 13                    "title": {
 14                        "type": "string"
 15                    },
 16                    "release": {
 17                        "type": "string"
 18                    },
 19                    "locatedIn": {
 20                        "$ref": "#/definitions/Library"
 21                    },
 22                    "writtenBy": {
 23                        "type": "array",
 24                        "items": {
 25                            "$ref": "#/definitions/Author"
 26                        }
 27                    }
 28                },
 29                "required": [
 30                ]
 31            }
 32        ]},
 33        "Author": {
 34            "allOf": [{
 35                "type": "object",
 36                "properties": {
 37                    "email": {
 38                        "type": "string"
 39                    },
 40                    "name": {
 41                        "type": "string"
 42                    },
 43                    "publishes": {
 44                        "type": "array",
 45                        "items": {
 46                            "$ref": "#/definitions/Book"
 47                        }
 48                    }
 49                },
 50                "required": [
 51                ]
 52            }
 53        ]},
 54        "Library": {
 55            "allOf": [{
 56                "type": "object",
 57                "properties": {
 58                    "name": {
 59                        "type": "string"
 60                    },
 61                    "address": {
 62                        "type": "string"
 63                    },
 64                    "has": {
 65                        "type": "array",
 66                        "items": {
 67                            "$ref": "#/definitions/Book"
 68                        }
 69                    }
 70                },
 71                "required": [
 72                ]
 73            }
 74        ]}
 75    },
 76    "definitions": {
 77        "Book": {
 78            "allOf": [{
 79                "type": "object",
 80                "properties": {
 81                    "pages": {
 82                        "type": "integer"
 83                    },
 84                    "title": {
 85                        "type": "string"
 86                    },
 87                    "release": {
 88                        "type": "string"
 89                    },
 90                    "locatedIn": {
 91                        "$ref": "#/definitions/Library"
 92                    },
 93                    "writtenBy": {
 94                        "type": "array",
 95                        "items": {
 96                            "$ref": "#/definitions/Author"
 97                        }
 98                    }
 99                },
100                "required": [
101                ]
102            }
103        ]},
104        "Author": {
105            "allOf": [{
106                "type": "object",
107                "properties": {
108                    "email": {
109                        "type": "string"
110                    },
111                    "name": {
112                        "type": "string"
113                    },
114                    "publishes": {
115                        "type": "array",
116                        "items": {
117                            "$ref": "#/definitions/Book"
118                        }
119                    }
120                },
121                "required": [
122                ]
123            }
124        ]},
125        "Library": {
126            "allOf": [{
127                "type": "object",
128                "properties": {
129                    "name": {
130                        "type": "string"
131                    },
132                    "address": {
133                        "type": "string"
134                    },
135                    "has": {
136                        "type": "array",
137                        "items": {
138                            "$ref": "#/definitions/Book"
139                        }
140                    }
141                },
142                "required": [
143                ]
144            }
145        ]}
146    }
147}

This schema can now be used by any JSON Schema validator to ensure that the JSON objects conform to the model’s structure and constraints.