Examples converting XSD to JSON

This section illustrates how to use XsdToJsonSchema and convert XSD (Xml Schema Document) into JSON schema.

Table of contents

Setup

After Setup the library will be installed by default under %ProgramFiles%\XsdToJsonSchema 1.1\

The set of DLL files are available at that location targeting .NET Framework 4.5 and above (folder XsdToJsonSchema 1.1\Bin\NET45\), .NET Standard 2.0 (folder XsdToJsonSchema 1.1\Bin\NetStandard20\) and .NET Standard 2.1 (folder XsdToJsonSchema 1.1\Bin\NetStandard21\)

In order to use the library, you have to reference the assembly file XsdToJsonSchema.dll from your .NET project and the assembly DLL must be deployed with your application.

Converting from XSD to JSON schema

The XSD to JSON conversion logic is available as a set of static methods in the XsdToJsonSchemaConverter class or as instance methods in the XsdObject class:

This sample shows how to convert an XSD schema available as a String variable.

using System.IO;
using XsdToJsonSchema;
class ConvertStaticDemo 
{
  public string ConverFromFile(string xsdFile)
  {
    string xsd = File.ReadAllText(xsdFile); 
    string jsonSchema = XsdToJsonSchema.XsdToJsonSchemaConverter.ConvertXsdToJsonSchema(xsd); 
    return jsonSchema;
  }
}

Converting from file

If the XSD is in a file and we want the conversion result to be also in a file we can use one of the overloaded methods for this purpose:

FileInfo xsdFile = new FileInfo(xsdFilePath);
FileInfo jsonFile = new FileInfo(jsonFilePath);
XsdToJsonSchema.XsdToJsonSchemaConverter.ConvertXsdToJsonSchema(xsdFile, jsonFile);

Converting from Stream

In case that the XSD schema is in some other kind of storage, for example, a database, then we can load the XSD schema contents into a System.IO.Stream and then convert it to a JSON schema into another Stream.

Stream xsd = ...
Stream json = ...
XsdToJsonSchema.XsdToJsonSchemaConverter.ConvertXsdToJsonSchema(xsd, json);

Converting with the XsdObject class

The XsdObject class from the same XsdToJsonSchema namespace offers similar instance methods. Below is a short example that illustrates basic usage flow:

using System.IO;
using XsdToJsonSchema;
class ConvertDemo 
{
  public string ConverFromFile(string xsdFile)
  {
    string xsd = File.ReadAllText(xsdFile); 
    XsdObject xsdObj = XsdObject.Create(xsd)
    bool prettyPrint = true;
    string jsonSchema = xsd.ToJsonSchema(prettyPrint)
    return jsonSchema;
  }
}

The $schema, $id, title and description properties

JSON schema has a set of attributes that can be set via properties of either the XsdToJsonSchemaConverter class or with the XsdObject:

XsdToJsonSchemaConverter.Schema = "https://json-schema.org/draft/2020-12/schema";
XsdToJsonSchemaConverter.Id = ...
XsdToJsonSchemaConverter.Title = ..
XsdToJsonSchemaConverter.Description = ...

string xsd = File.ReadAllText(xsdFilePath); 
XsdObject xsdObj = XsdObject.Create(xsd);
xsdObj.Schema = "//json-schema.org/draft/2020-12/schema";
xsdObj.Id = ...
xsdObj.Title = ...
xsdObj.Description = ...