UML to JSON Schema

The source code generation tool supports generating JSON schema from a UML class diagram.

JSON Schema from UML Diagram

UML class diagram can be used to produce a JSON schema. You can specify classes that represent parts of the JSON schema. Source code generation feature in Software Ideas Modeler allows you to define types for the particular attributes, generalizations, and compositions.

Root Element

A class in the diagram for JSON schema should be marked as root. The schema generation will start from this element. If you want to set the desired class as the root element, select it first. Then right-click on it and choose the Root item from the submenu Modifiers in the context menu.

Required Attributes

You can also specify required attributes using the «required» stereotype. Select the desired attribute or attributes, right-click on one of the selected attributes and choose Stereotypes/Required from the context menu.

Description

You can define the description for each JSON schema element using the documentation. If you want to edit the documentation right-click on the class element or its attribute and choose Properties from the context menu. Then you can switch to the Documentation tab and enter the element description.

How to Generate JSON Schema

If you want to generate a JSON schema from a UML class diagram, you can do it this way:

1. Open the class diagram

2. Click on the Source Code button in the Generation group on the Process ribbon tab (ribbon: Process / Generation / Source Code). If you use the menu interface, you can find the option under Tools menu / Generate Source Code.

3. In the Source Code Generation dialog, choose JSON from the Language drop-down and Default from the template drop-down. Enter the folder to which you want to generate the JSON schema files to the Output directory box.

4. Click on the Generate button

JSON Schema Diagram Example

UML class diagram for JSON schema
UML class diagram for JSON schema

For the class diagram above will be generated the following JSON schema:

{
    "$schema": "http://json-schema.org/draft/2019-09/schema",
    "title": "JsonElement",
    "description": "Description of JSONElement can be defined here.",
    "type": "object",
    "required": [   "RequiredAttribute"],
    "properties": {
        "NumericAttribute" : {
            "type": "number",
            "description": ""
        }, 
        "TextAttribute" : {
            "type": "string",
            "description": ""
        }, 
        "RequiredAttribute" : {
            "type": "string",
            "description": ""
        }, 
        "nestedElements" : {
            "type": "array",
            "description": "",
            "required": [   ],
            "properties": {
                "BoolAttribute" : {
                    "type": "bool",
                    "description": ""
                }
            }
            "description": ""
        }
    }
}

    

New Comment

Comment