Enterprise Architect Model transformation
Transforming UML Class Diagram into JSON Schema



Resources
Example

Class code template (customized for JSON Schema language)

{
	%PI=""%
	%qt%%dl%comment%qt%: %qt%(c) BarbierDarnal.com%qt%,
	$COMMENT = "'$','\'... are reserved characters"
	$class_name = %REPLACE(className," ","_")%
	$package_name = %REPLACE(packageName," ","_")%
	%qt%%dl%id%qt%: %qt%https://BarbierDarnal.com/JSON_Schema/$package_name/$class_name%qt%,
	%qt%description%qt%: %qt%%classNotes%%qt%,
	%qt%title%qt%: %qt%%className%%qt%,
	%qt%type%qt%: %qt%object%qt%,
	%qt%version%qt%: %qt%%classVersion%%qt%,
	%qt%properties%qt%: {
		%ClassBody(className,classGUID)%
	},
	$attributes = %list="Attribute__Required" @separator="," @indent=""%
	$attributes = %TRIM($attributes)%
	$connectors = %list="Connector__Required" @separator="," @indent=""%
	$connectors = %TRIM($connectors)%
	%qt%required%qt%: [
		%if $attributes != ""%
			%if $connectors != ""%
				$attributes,
				$connectors
			%else%
				$attributes
			%endIf%
		%elseIf $connectors != ""%
			$connectors
		%endIf%
	]
}

Note that code (generation) templates may be exported as reference data

Transformation result: Company.schema.json file

{
    "$comment": "(c) BarbierDarnal.com",
    "$id": "https://BarbierDarnal.com/JSON_Schema/Hyperlink/Company",
    "description": "Company notes...",
    "title": "Company",
    "type": "object",
    "version": "1.0",
    "properties": {
        "location": {
            "description": "",
            "type": "object"
        },
        "name": {
            "description": "",
            "type": "string",
            "format": "uuid"
        },
        "web": {
            "description": "",
            "type": "string"
        },
        "owner": {
            "description": " --- ",
            "$ref": "/JSON_Schema/Hyperlink/People"
        }
    },
    "required": ["name", "web", "owner"]
}
UML Composition

Connector__Composition code template (added to JSON Schema language)…

%if connectorType != "Aggregation" or connectorDestAggregation != "composite" and connectorSourceAggregation != "composite"%
%endTemplate%
$COMMENT = "UML Composition leads to nested data structures in JSON Schema"
$GUID = ""
$multiplicity = ""
$name = ""
$navigability = ""
$type = ""
$visibility = ""
%if className == connectorDestElemName and connectorDestAggregation == "composite"%
	$GUID = %connectorSourceElemGUID%
	$multiplicity = %connectorSourceMultiplicity%
	$navigability = %connectorSourceNavigability%
	$type = %connectorSourceElemName%
	$visibility = %connectorSourceAccess%
	%if connectorSourceRole == ""%
		$name = %TO_LOWER(connectorSourceElemName)%
	%else%
		$name = %TO_LOWER(connectorSourceRole)%
	%endIf%
%else%
	%if className == connectorSourceElemName and connectorSourceAggregation == "composite"%
		$GUID = %connectorDestElemGUID%
		$multiplicity = %connectorDestMultiplicity%
		$navigability = %connectorDestNavigability%
		$type = %connectorDestElemName%
		$visibility = %connectorDestAccess%
		%if connectorDestRole == ""%
			$name = %TO_LOWER(connectorDestElemName)%
		%else%
			$name = %TO_LOWER(connectorDestRole)%	
		%endIf%
	%endIf%
%endIf%
%if $name != "" and $type != "" and $navigability != "Non-Navigable" and $visibility == "Public"%
	%if $multiplicity == "0..1" or $multiplicity == "1..1" or $multiplicity == "1"%
		%qt%$name%qt%: {
			$COMMENT = "https://sparxsystems.com/enterprise_architect_user_guide/17.0/modeling_domains/functionmacros.html"
			%EXEC_ADD_IN("Attributes", "attributes", $GUID, "Optional data here")%
		}
	%else%
		%qt%$name%qt%: {
			%qt%type%qt%: %qt%array%qt%
		}
	%endIf%
%endIf%

Transformation result: People.schema.json file

{
    "$comment": "(c) BarbierDarnal.com",
    "$id": "https://BarbierDarnal.com/JSON_Schema/Hyperlink/People",
    "description": "",
    "title": "People",
    "type": "object",
    "version": "1.0",
    "properties": {
        "brain": {
            "weight": {
                "description": "Heavy if possible...",
                "type": "object"
            }
        }
    },
    "required": ["brain"]
}
Validation

To be done with Python