The JSON Schema Generator creates a JSON Schema from an XML Schema (XSD)
The JSON Schema Generator tool uses a Wizard to create a compliant JSON Schema by inferring its structure from a sample XML Schema. Configuration options control the rules used when inferring the schemas structure.
The pipeline for rapid JOSN Schema creation is as follows:
Why use a JSON Schema?
- Provides a formal, unambiguous description, essential for distribution to 3rd parties
- Validation of JSON documents.
- JSON development tools use them to provide intellisense and autocomplete
- Reduces the amount of validation code needed in client applications
- Basis for generating a data layer for your application (JSON Data Binding)
- Forms the basis of formal documentation for your data model
Example - Converting an XML Schema (XSD) to a JSON Schema
We will use the following sample XML Schema to demonstrate the effect of the Generation Options:
<?xml version="1.0" encoding="utf-8" ?> <!--Created with Liquid Studio (https://www.liquid-technologies.com)--> <xsd:schema xmlns:bs="http://www.liquid-technologies.com/sample/bookstore" elementFormDefault="qualified" targetNamespace="http://www.liquid-technologies.com/sample/bookstore" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:complexType name="bookstoreType"> <xsd:sequence minOccurs="0" maxOccurs="unbounded"> <xsd:element name="book" type="bs:bookType" /> </xsd:sequence> </xsd:complexType> <xsd:complexType name="bookType"> <xsd:sequence> <xsd:element name="title"> <xsd:annotation> <xsd:documentation>The title of the book. Max 50 characters.</xsd:documentation> </xsd:annotation> <xsd:simpleType> <xsd:restriction base="xsd:string"> <xsd:maxLength value="50" /> </xsd:restriction> </xsd:simpleType> </xsd:element> <xsd:element name="author" type="bs:authorName" /> <xsd:element name="genre" type="xsd:string" minOccurs="0" /> </xsd:sequence> <xsd:attribute name="price" type="xsd:double" use="required" /> <xsd:attribute name="publicationdate" type="xsd:date" /> <xsd:attribute name="ISBN" type="xsd:string" /> </xsd:complexType> <xsd:element name="bookstore" type="bs:bookstoreType" /> <xsd:complexType name="authorName"> <xsd:sequence> <xsd:element name="first-name" type="xsd:string"> <xsd:annotation> <xsd:documentation>The authors first name. Max 50 characters.</xsd:documentation> </xsd:annotation> </xsd:element> <xsd:element name="last-name" type="xsd:string" /> </xsd:sequence> </xsd:complexType> </xsd:schema>
The following JSON Schema is created from the sample XSD data above.
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "bookstore": { "$ref": "#/$defs/bookstore" } }, "additionalProperties": false, "required": [ "bookstore" ], "$defs": { "bookstore": { "$ref": "#/$defs/bookstoreType" }, "bookstoreType": { "type": "object", "properties": { "book": { "type": "array", "items": { "$ref": "#/$defs/bookType" } } }, "additionalProperties": false }, "bookType": { "type": "object", "properties": { "price": { "type": "number" }, "publicationdate": { "type": "string" }, "ISBN": { "type": "string" }, "title": { "type": "string", "description": "The title of the book. \nMax 50 characters.", "maxLength": 50 }, "author": { "$ref": "#/$defs/authorName" }, "genre": { "type": "string" } }, "additionalProperties": false, "required": [ "price", "title", "author" ] }, "authorName": { "type": "object", "properties": { "first-name": { "type": "string", "description": "The authors first name.\nMax 50 characters." }, "last-name": { "type": "string" } }, "additionalProperties": false, "required": [ "first-name", "last-name" ] } } }
Once you have your JSON Schema the way you want it, you can test it out by using it to validate some JSON data you plan on using with your project. With the JSON file open, add the JSON Schema to the document by selecting "Attach Schema" from the toolbar or Tools menu and browsing to a JSON Schema file you inferred. Once the JSON Schema has been cited within the document, clicking the Validate button or choosing it from the Tools menu will check the XML against the generated JSON Schema.
Once you have a JSON Schema added to a JSON document, you will see Schema-aware intellisense as you edit the data in the source view, prompting you with elements and attributes appropriate to the structures defined within the Schema, relative to your position in the data.