Ecore Metamodeling

JSON and XMI Formats

Learn how to convert models between JSON and XMI formats, and validate round-trip conversions.

In this tutorial, you’ll explore both serialisation formats supported by swift-ecore, understand their trade-offs, and master the conversion tools for working with models in different formats.

30 mins Estimated Time

Section 1

Understanding Serialisation Formats

swift-ecore supports two serialisation formats: XMI (XML Metadata Interchange) and JSON. XMI is the standard EMF format with rich metadata, whilst JSON provides a lightweight, modern alternative.

Choose XMI for interoperability with Eclipse EMF tools, or JSON for web applications and modern workflows. Both formats preserve complete model structure and can be converted losslessly.

Step 1

Start with a Company model in XMI format.

This XMI file contains a Company with Employee and Manager instances, using the advanced features from Tutorial 04.

company-model.xmi
1<?xml version="1.0" encoding="UTF-8"?>
2<xmi:XMI xmi:version="2.0"
3 xmlns:xmi="http://www.omg.org/XMI"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xmlns:company="http://www.example.org/company">
6
7 <company:Company xmi:id="company1" name="Tech Innovations Ltd">
8 <employees xsi:type="company:Employee" xmi:id="emp1" name="Alice Johnson" email="alice@techinnovations.com" status="FULL_TIME" employeeId="E001"/>
9 <employees xsi:type="company:Employee" xmi:id="emp2" name="Bob Smith" email="bob@techinnovations.com" status="PART_TIME" employeeId="E002"/>
10 <employees xsi:type="company:Manager" xmi:id="mgr1" name="Carol Williams" email="carol@techinnovations.com" status="FULL_TIME" department="Engineering"/>
11 </company:Company>
12
13</xmi:XMI>

Step 2

Convert the XMI model to JSON format.

The convert command transforms XMI to JSON whilst preserving all model data, including type information and cross-references.

Terminal
1swift-ecore convert company-model.xmi company-model.json

Step 3

Examine the JSON representation.

The JSON format uses $type for metamodel types and $id for identifiers. It’s more concise than XMI and easier to work with in modern tools.

company-model.json
1{
2 "$type": "company:Company",
3 "$id": "company1",
4 "name": "Tech Innovations Ltd",
5 "employees": [
6 {
7 "$type": "company:Employee",
8 "$id": "emp1",
9 "name": "Alice Johnson",
10 "email": "alice@techinnovations.com",
11 "status": "FULL_TIME",
12 "employeeId": "E001"
13 },
14 {
15 "$type": "company:Employee",
16 "$id": "emp2",
17 "name": "Bob Smith",
18 "email": "bob@techinnovations.com",
19 "status": "PART_TIME",
20 "employeeId": "E002"
21 },
22 {
23 "$type": "company:Manager",
24 "$id": "mgr1",
25 "name": "Carol Williams",
26 "email": "carol@techinnovations.com",
27 "status": "FULL_TIME",
28 "department": "Engineering"
29 }
30 ]
31}

Section 2

Converting Between Formats

Converting between JSON and XMI is bidirectional and lossless. You can freely move between formats depending on your workflow needs.

Use swift-ecore convert with appropriate input and output file extensions—the tool automatically detects and handles both formats.

Step 1

Convert JSON back to XMI format.

Converting from JSON to XMI produces standard EMF-compatible XMI that can be opened in Eclipse or other EMF tools.

Terminal
1swift-ecore convert company-model.json company-roundtrip.xmi

Step 2

Compare the round-trip result with the original.

The diff command should show minimal or no differences. Format-specific details like whitespace or attribute order may vary, but semantic content remains identical.

Terminal
1diff company-model.xmi company-roundtrip.xmi

Section 3

Format Trade-offs and Best Practices

Each format has specific advantages. XMI provides comprehensive metadata and cross-resource references, making it ideal for complex model repositories. JSON offers simplicity and modern tooling support.

Consider your workflow: use XMI when working with Eclipse EMF tools or complex model structures, and JSON for web applications, APIs, or when readability matters.

Step 1

Validate a JSON model against its metamodel.

The validate command works with both formats, checking type conformance, cardinality constraints, and reference integrity.

Terminal
1swift-ecore validate company-model.json --metamodel Company.ecore

Step 2

Inspect a JSON model structure.

All swift-ecore commands accept both JSON and XMI inputs interchangeably, providing a unified experience regardless of format.

Terminal
1swift-ecore inspect company-model.json --detail full

Step 3

Use format conversion in your workflow.

Batch convert multiple models using shell scripting. This example converts all XMI files in a directory to JSON.

Terminal
1for file in *.xmi; do
2 swift-ecore convert "$file" "${file%.xmi}.json"
3done

Check Your Understanding

Question 1 of 4

What is the primary difference between XMI and JSON formats?

Question 2 of 4

When should you use XMI format instead of JSON?

Question 3 of 4

What does “round-trip conversion” mean?

Question 4 of 4

How does swift-ecore determine input format when converting?

Querying Models with AQL

Learn how to query and navigate models using AQL (ATLAS Query Language), including element selection, collection operations, and complex queries.