NJsonSchema.CodeGeneration.TypeScript 11.6.1

NJsonSchema for .NET

NSwag | NJsonSchema | Apimundo | Namotion.Reflection

Azure DevOps Nuget Discord StackOverflow Wiki Apimundo

NJsonSchema is a .NET library to read, generate and validate JSON Schema draft v4+ schemas. The library can read a schema from a file or string and validate JSON data against it. A schema can also be generated from an existing .NET class. With the code generation APIs you can generate C# and TypeScript classes or interfaces from a schema.

The library uses Json.NET to read and write JSON data and Namotion.Reflection for additional .NET reflection APIs.

NuGet packages:

Preview NuGet Feed: https://www.myget.org/F/njsonschema/api/v3/index.json

Features:

NJsonSchema is heavily used in NSwag, a Swagger API toolchain for .NET which generates client code for Web API services. NSwag also provides command line tools to use the NJsonSchema's JSON Schema generator (command types2swagger).

The project is developed and maintained by Rico Suter and other contributors.

Some code generators can directly be used via the Apimundo service.

NJsonSchema usage

The JsonSchema class can be used as follows:

var schema = JsonSchema.FromType<Person>();
var schemaData = schema.ToJson();
var errors = schema.Validate("{...}");

foreach (var error in errors)
    Console.WriteLine(error.Path + ": " + error.Kind);

schema = await JsonSchema.FromJsonAsync(schemaData);

The Person class:

public class Person
{
    [Required]
    public string FirstName { get; set; }

    public string MiddleName { get; set; }

    [Required]
    public string LastName { get; set; }

    public Gender Gender { get; set; }

    [Range(2, 5)]
    public int NumberWithRange { get; set; }

    public DateTime Birthday { get; set; }

    public Company Company { get; set; }

    public Collection<Car> Cars { get; set; }
}

public enum Gender
{
    Male,
    Female
}

public class Car
{
    public string Name { get; set; }

    public Company Manufacturer { get; set; }
}

public class Company
{
    public string Name { get; set; }
}

The generated JSON schema data stored in the schemaData variable:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Person",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "FirstName",
    "LastName"
  ],
  "properties": {
    "FirstName": {
      "type": "string",
      "minLength": 1
    },
    "MiddleName": {
      "type": [
        "null",
        "string"
      ]
    },
    "LastName": {
      "type": "string",
      "minLength": 1
    },
    "Gender": {
      "$ref": "#/definitions/Gender"
    },
    "NumberWithRange": {
      "type": "integer",
      "format": "int32",
      "maximum": 5.0,
      "minimum": 2.0
    },
    "Birthday": {
      "type": "string",
      "format": "date-time"
    },
    "Company": {
      "oneOf": [
        {
          "type": "null"
        },
        {
          "$ref": "#/definitions/Company"
        }
      ]
    },
    "Cars": {
      "type": [
        "array",
        "null"
      ],
      "items": {
        "$ref": "#/definitions/Car"
      }
    }
  },
  "definitions": {
    "Gender": {
      "type": "integer",
      "description": "",
      "x-enumNames": [
        "Male",
        "Female"
      ],
      "enum": [
        0,
        1
      ]
    },
    "Company": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Name": {
          "type": [
            "null",
            "string"
          ]
        }
      }
    },
    "Car": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "Name": {
          "type": [
            "null",
            "string"
          ]
        },
        "Manufacturer": {
          "oneOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/definitions/Company"
            }
          ]
        }
      }
    }
  }
}

NJsonSchema.CodeGeneration usage

The NJsonSchema.CodeGeneration can be used to generate C# or TypeScript code from a JSON schema:

var generator = new CSharpGenerator(schema);
var file = generator.GenerateFile();

The file variable now contains the C# code for all the classes defined in the JSON schema.

TypeScript

The previously generated JSON Schema would generate the following TypeScript interfaces.

Settings:

new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Interface, TypeScriptVersion = 4.3m }

Output:

export enum Gender {
    Male = 0, 
    Female = 1, 
}

export interface Company {
    Name: string | undefined;
}

export interface Car {
    Name: string | undefined;
    Manufacturer: Company | undefined;
}

export interface Person {
    FirstName: string;
    MiddleName: string | undefined;
    LastName: string;
    Gender: Gender;
    NumberWithRange: number;
    Birthday: Date;
    Company: Company | undefined;
    Cars: Car[] | undefined;
}

... and the following TypeScript classes.

Settings:

new TypeScriptGeneratorSettings { TypeStyle = TypeScriptTypeStyle.Class, TypeScriptVersion = 4.3m }

Output:

export enum Gender {
    Male = 0, 
    Female = 1, 
}

export class Company implements ICompany {
    name: string | undefined;

    constructor(data?: ICompany) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(data?: any) {
        if (data) {
            this.name = data["Name"];
        }
    }

    static fromJS(data: any): Company {
        let result = new Company();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["Name"] = this.name;
        return data; 
    }
}

export interface ICompany {
    name: string | undefined;
}

export class Car implements ICar {
    name: string | undefined;
    manufacturer: Company | undefined;

    constructor(data?: ICar) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(data?: any) {
        if (data) {
            this.name = data["Name"];
            this.manufacturer = data["Manufacturer"] ? Company.fromJS(data["Manufacturer"]) : <any>undefined;
        }
    }

    static fromJS(data: any): Car {
        let result = new Car();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["Name"] = this.name;
        data["Manufacturer"] = this.manufacturer ? this.manufacturer.toJSON() : <any>undefined;
        return data; 
    }
}

export interface ICar {
    name: string | undefined;
    manufacturer: Company | undefined;
}

export class Person implements IPerson {
    firstName: string;
    middleName: string | undefined;
    lastName: string;
    gender: Gender;
    numberWithRange: number;
    birthday: Date;
    company: Company | undefined;
    cars: Car[] | undefined;

    constructor(data?: IPerson) {
        if (data) {
            for (var property in data) {
                if (data.hasOwnProperty(property))
                    (<any>this)[property] = (<any>data)[property];
            }
        }
    }

    init(data?: any) {
        if (data) {
            this.firstName = data["FirstName"];
            this.middleName = data["MiddleName"];
            this.lastName = data["LastName"];
            this.gender = data["Gender"];
            this.numberWithRange = data["NumberWithRange"];
            this.birthday = data["Birthday"] ? new Date(data["Birthday"].toString()) : <any>undefined;
            this.company = data["Company"] ? Company.fromJS(data["Company"]) : <any>undefined;
            if (data["Cars"] && data["Cars"].constructor === Array) {
                this.cars = [];
                for (let item of data["Cars"])
                    this.cars.push(Car.fromJS(item));
            }
        }
    }

    static fromJS(data: any): Person {
        let result = new Person();
        result.init(data);
        return result;
    }

    toJSON(data?: any) {
        data = typeof data === 'object' ? data : {};
        data["FirstName"] = this.firstName;
        data["MiddleName"] = this.middleName;
        data["LastName"] = this.lastName;
        data["Gender"] = this.gender;
        data["NumberWithRange"] = this.numberWithRange;
        data["Birthday"] = this.birthday ? this.birthday.toISOString() : <any>undefined;
        data["Company"] = this.company ? this.company.toJSON() : <any>undefined;
        if (this.cars && this.cars.constructor === Array) {
            data["Cars"] = [];
            for (let item of this.cars)
                data["Cars"].push(item.toJSON());
        }
        return data; 
    }
}

export interface IPerson {
    firstName: string;
    middleName: string | undefined;
    lastName: string;
    gender: Gender;
    numberWithRange: number;
    birthday: Date;
    company: Company | undefined;
    cars: Car[] | undefined;
}

NJsonSchema.SampleJsonSchemaGenerator usage

The NJsonSchema.SampleJsonSchemaGenerator can be used to generate a JSON Schema from sample JSON data:

JSON Schema Specification

By default, the NJsonSchema.SampleJsonSchemaGenerator generates a JSON Schema based on the JSON Schema specification. See: JSON Schema Specification

var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings());

var schema = generator.Generate("{...}");

Input:

{
  "int": 1,
  "float": 340282346638528859811704183484516925440.0,
  "str": "abc",
  "bool": true,
  "date": "2012-07-19",
  "datetime": "2012-07-19 10:11:11",
  "timespan": "10:11:11"
}

Output:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "int": {
      "type": "integer"
    },
    "float": {
      "type": "number"
    },
    "str": {
      "type": "string"
    },
    "bool": {
      "type": "boolean"
    },
    "date": {
      "type": "string",
      "format": "date"
    },
    "datetime": {
      "type": "string",
      "format": "date-time"
    },
    "timespan": {
      "type": "string",
      "format": "duration"
    }
  }
}

OpenApi Specification

To generate a JSON Schema for OpenApi, provide the SchemaType.OpenApi3 in the settings. See: OpenApi Specification

var generator = new SampleJsonSchemaGenerator(new SampleJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });

var schema = generator.Generate("{...}");

Input:

{
  "int": 12345,
  "long": 1736347656630,
  "float": 340282346638528859811704183484516925440.0,
  "double": 340282346638528859811704183484516925440123456.0,
}

Output:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "int": {
      "type": "integer",
      "format": "int32"
    },
    "long": {
      "type": "integer",
      "format": "int64"
    },
    "float": {
      "type": "number",
      "format": "float"
    },
    "double": {
      "type": "number",
      "format": "double"
    }
  }
}

Final notes

Applications which use the library:

Showing the top 20 packages that depend on NJsonSchema.CodeGeneration.TypeScript.

Packages Downloads
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
25
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
26
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
28
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
25
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
26
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
27

.NET Framework 4.6.2

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
11.6.1 2 05/05/2026
11.6.0 3 04/11/2026
11.5.2 10 11/07/2025
11.5.1 9 10/03/2025
11.5.0 10 09/23/2025
11.4.0 10 08/02/2025
11.3.2 13 06/14/2025
11.3.1 12 06/15/2025
11.3.0 14 06/15/2025
11.2.0 14 06/15/2025
11.1.0 15 01/23/2025
11.0.2 14 01/23/2025
11.0.1 14 01/23/2025
11.0.0 16 05/25/2024
11.0.0-preview008 16 05/26/2024
11.0.0-preview007 14 05/27/2024
11.0.0-preview006 16 05/28/2024
11.0.0-preview005 16 05/28/2024
11.0.0-preview004 18 05/28/2024
11.0.0-preview003 18 05/28/2024
11.0.0-preview002 15 05/28/2024
11.0.0-preview001 17 05/27/2024
10.9.0 16 05/27/2024
10.8.0 17 05/28/2024
10.7.2 16 05/28/2024
10.7.1 15 05/28/2024
10.7.0 17 05/28/2024
10.6.10 17 05/28/2024
10.6.9 15 05/27/2024
10.6.8 16 05/28/2024
10.6.7 17 05/27/2024
10.6.6 17 05/26/2024
10.6.5 17 05/28/2024
10.6.4 17 05/27/2024
10.6.3 16 05/27/2024
10.6.2 17 05/27/2024
10.6.1 19 05/27/2024
10.6.0 18 05/27/2024
10.5.2 17 05/26/2024
10.5.1 14 05/27/2024
10.5.0 18 05/27/2024
10.4.6 18 05/27/2024
10.4.5 15 05/28/2024
10.4.4 15 05/28/2024
10.4.3 16 05/28/2024
10.4.2 14 05/27/2024
10.4.1 17 05/27/2024
10.4.0 18 05/27/2024
10.3.11 17 05/28/2024
10.3.10 16 05/28/2024
10.3.9 16 05/28/2024
10.3.8 17 05/27/2024
10.3.7 17 05/27/2024
10.3.6 20 05/28/2024
10.3.5 23 05/27/2024
10.3.4 17 05/28/2024
10.3.3 14 05/28/2024
10.3.2 16 05/28/2024
10.3.1 17 05/27/2024
10.3.0 18 05/28/2024
10.2.2 17 05/27/2024
10.2.1 17 05/26/2024
10.2.0 17 05/28/2024
10.1.26 16 05/27/2024
10.1.25 15 05/26/2024
10.1.24 15 05/28/2024
10.1.23 16 05/27/2024
10.1.22 20 05/28/2024
10.1.21 19 05/28/2024
10.1.20 16 05/26/2024
10.1.19 18 05/28/2024
10.1.18 17 05/27/2024
10.1.17 16 05/27/2024
10.1.16 16 05/27/2024
10.1.15 16 05/27/2024
10.1.14 18 05/26/2024
10.1.13 16 05/28/2024
10.1.12 15 05/28/2024
10.1.11 18 05/27/2024
10.1.10 18 05/27/2024
10.1.9 20 05/27/2024
10.1.8 18 05/26/2024
10.1.7 17 05/27/2024
10.1.6 17 05/28/2024
10.1.5 17 05/27/2024
10.1.4 17 05/26/2024
10.1.3 15 05/27/2024
10.1.2 17 05/28/2024
10.1.1 16 05/28/2024
10.1.0 16 05/27/2024
10.0.28 15 05/28/2024
10.0.27 17 05/27/2024
10.0.26 16 05/28/2024
10.0.25 17 05/28/2024
10.0.24 15 05/27/2024
10.0.23 17 05/26/2024
10.0.22 16 05/27/2024
10.0.21 16 05/27/2024
10.0.20 20 05/26/2024
10.0.19 17 05/28/2024
10.0.18 18 05/28/2024
10.0.17 16 05/28/2024
10.0.16 15 05/27/2024
10.0.15 16 05/27/2024
10.0.14 16 05/28/2024
10.0.13 18 05/27/2024
10.0.12 18 05/27/2024
10.0.11 15 05/27/2024
10.0.10 18 05/27/2024
10.0.9 18 05/27/2024
10.0.8 16 05/27/2024
10.0.7 15 05/28/2024
10.0.6 17 05/29/2024
10.0.5 17 05/27/2024
10.0.4 15 05/28/2024
10.0.3 15 05/27/2024
10.0.1 16 05/27/2024
10.0.0 17 05/27/2024
9.14.1 16 05/27/2024
9.14.0 16 05/27/2024
9.13.37 15 05/27/2024
9.13.36 15 05/28/2024
9.13.35 14 05/27/2024
9.13.34 18 05/27/2024
9.13.33 14 05/27/2024
9.13.32 16 05/27/2024
9.13.31 16 05/28/2024
9.13.30 15 05/28/2024
9.13.29 16 05/27/2024
9.13.28 16 05/27/2024
9.13.27 16 05/26/2024
9.13.26 17 05/28/2024
9.13.25 16 05/28/2024
9.13.24 15 05/27/2024
9.13.23 18 05/28/2024
9.13.22 14 05/27/2024
9.13.19 15 05/27/2024
9.13.18 16 05/27/2024
9.13.17 15 05/28/2024
9.13.16 16 05/27/2024
9.13.15 14 05/27/2024
9.13.14 14 05/28/2024
9.13.13 14 05/27/2024
9.13.12 16 05/27/2024
9.13.11 15 05/26/2024
9.13.10 15 05/28/2024
9.13.9 17 05/27/2024
9.13.8 15 05/28/2024
9.13.7 15 05/27/2024
9.13.5 13 05/28/2024
9.13.4 17 05/28/2024
9.13.3 17 05/28/2024
9.13.2 17 05/27/2024
9.13.1 18 05/27/2024
9.13.0 17 05/27/2024
9.12.7 14 05/27/2024
9.12.6 15 05/27/2024
9.12.5 19 05/28/2024
9.12.4 17 05/28/2024
9.12.3 14 05/28/2024
9.12.2 14 05/27/2024
9.12.1 15 05/28/2024
9.12.0 16 05/28/2024
9.11.1 17 05/27/2024
9.11.0 14 05/27/2024
9.10.75 16 05/27/2024
9.10.74 15 05/28/2024
9.10.73 17 05/26/2024
9.10.72 16 05/27/2024
9.10.71 14 05/27/2024
9.10.70 17 05/27/2024
9.10.69 15 05/27/2024
9.10.68 15 05/27/2024
9.10.67 16 05/27/2024
9.10.66 15 05/27/2024
9.10.65 15 05/27/2024
9.10.64 15 05/27/2024
9.10.63 17 05/27/2024
9.10.62 14 05/27/2024
9.10.61 17 05/27/2024
9.10.60 15 05/27/2024
9.10.59 18 05/27/2024
9.10.58 15 05/27/2024
9.10.57 16 05/27/2024
9.10.56 14 05/28/2024
9.10.55 15 05/28/2024
9.10.54 16 05/27/2024
9.10.53 16 05/27/2024
9.10.52 16 05/28/2024
9.10.51 16 05/27/2024
9.10.50 16 05/28/2024
9.10.49 16 05/28/2024
9.10.48 14 05/27/2024
9.10.47 16 05/27/2024
9.10.46 16 05/27/2024
9.10.45 15 05/28/2024
9.10.44 15 05/28/2024
9.10.43 16 05/28/2024
9.10.42 24 05/27/2024
9.10.41 16 05/26/2024
9.10.40 16 05/28/2024
9.10.39 16 05/27/2024
9.10.38 17 05/28/2024
9.10.37 15 05/28/2024
9.10.36 15 05/27/2024
9.10.35 17 05/27/2024
9.10.34 16 05/27/2024
9.10.33 16 05/27/2024
9.10.32 15 05/27/2024
9.10.31 22 05/27/2024
9.10.30 16 05/27/2024
9.10.29 15 05/26/2024
9.10.28 19 05/27/2024
9.10.27 15 05/27/2024
9.10.26 16 05/27/2024
9.10.25 15 05/27/2024
9.10.24 15 05/28/2024
9.10.23 16 05/27/2024
9.10.22 15 05/26/2024
9.10.21 15 05/28/2024
9.10.20 18 05/27/2024
9.10.19 15 05/28/2024
9.10.18 16 05/27/2024
9.10.17 16 05/27/2024
9.10.16 15 05/27/2024
9.10.15 17 05/27/2024
9.10.14 15 05/27/2024
9.10.13 15 05/28/2024
9.10.12 16 05/28/2024
9.10.11 16 05/27/2024
9.10.10 16 05/27/2024
9.10.9 16 05/27/2024
9.10.8 17 05/27/2024
9.10.7 16 05/27/2024
9.10.6 16 05/27/2024
9.10.5 16 05/27/2024
9.10.4 15 05/27/2024
9.10.3 17 05/27/2024
9.10.2 16 05/28/2024
9.10.1 15 05/28/2024
9.10.0 16 05/28/2024
9.9.17 14 05/26/2024
9.9.16 18 05/27/2024
9.9.15 14 05/27/2024
9.9.14 17 05/27/2024
9.9.13 15 05/26/2024
9.9.12 17 05/27/2024
9.9.11 15 05/28/2024
9.9.10 16 05/26/2024
9.9.9 16 05/27/2024
9.9.8 17 05/28/2024
9.9.7 17 05/27/2024
9.9.6 18 05/28/2024
9.9.5 15 05/28/2024
9.9.4 16 05/27/2024
9.9.3 16 05/26/2024
9.9.2 16 05/28/2024
9.9.1 16 05/26/2024
9.9.0 16 05/28/2024
9.8.3 15 05/28/2024
9.8.2 18 05/27/2024
9.8.1 15 05/27/2024
9.8.0 16 05/27/2024
9.7.7 17 05/28/2024
9.7.6 18 05/27/2024
9.7.5 20 05/27/2024
9.7.3 22 05/28/2024
9.7.2 17 05/27/2024
9.7.1 17 05/27/2024
9.7.0 15 05/27/2024
9.6.5 16 05/28/2024
9.6.4 14 05/26/2024
9.6.3 17 05/28/2024
9.6.2 16 05/27/2024
9.6.1 17 05/28/2024
9.6.0 19 05/27/2024
9.5.4 15 05/27/2024
9.5.3 16 05/28/2024
9.5.2 18 05/28/2024
9.5.1 15 05/28/2024
9.5.0 16 05/27/2024
9.4.10 17 05/26/2024
9.4.9 16 05/27/2024
9.4.8 16 05/28/2024
9.4.7 16 05/27/2024
9.4.6 15 05/27/2024
9.4.5 19 05/27/2024
9.4.4 15 05/27/2024
9.4.3 16 05/27/2024
9.4.2 16 05/28/2024
9.4.1 17 05/27/2024
9.4.0 18 05/27/2024
9.3.2 18 05/28/2024
9.3.1 17 05/28/2024
9.3.0 17 05/28/2024
9.2.5 16 05/27/2024
9.2.4 15 05/27/2024
9.2.3 18 05/27/2024
9.2.2 15 05/27/2024
9.2.1 15 05/28/2024
9.2.0 15 05/27/2024
9.1.13 14 05/28/2024
9.1.12 15 05/28/2024
9.1.11 15 05/28/2024
9.1.10 16 05/26/2024
9.1.9 17 05/27/2024
9.1.8 17 05/27/2024
9.1.7 19 05/26/2024
9.1.6 16 05/27/2024
9.1.5 16 05/28/2024
9.1.4 15 05/27/2024
9.1.3 16 05/28/2024
9.1.2 19 05/27/2024
9.1.1 16 05/27/2024
9.1.0 16 05/26/2024
9.0.0 16 05/27/2024
8.34.6331.29196 18 05/28/2024
8.33.6323.36234 15 05/27/2024
8.32.6319.16954 17 05/28/2024
8.31.6318.20496 17 05/27/2024
8.30.6304.31907 16 05/27/2024
8.29.6304.29598 15 05/26/2024
8.28.6304.29323 17 05/27/2024
8.27.6302.16061 16 05/27/2024
8.26.6300.23803 17 05/27/2024
8.25.6298.25745 15 05/27/2024
8.24.6298.12189 15 05/27/2024
8.23.6298.10833 16 05/28/2024
8.22.6296.25118 16 05/28/2024
8.21.6296.23976 17 05/27/2024
8.20.6296.23156 16 05/27/2024
8.19.6295.40488 17 05/27/2024
8.18.6295.39662 17 05/27/2024
8.17.6295.16512 14 05/27/2024
8.16.6295.13544 14 05/27/2024
8.15.6294.28833 16 05/26/2024
8.14.6289.34377 14 05/28/2024
8.13.6288.31202 15 05/26/2024
8.12.6288.29377 17 05/28/2024
8.11.6284.26878 17 05/27/2024
8.10.6282.29590 17 05/27/2024
8.9.6275.22314 15 05/27/2024
8.8.6270.12850 19 05/27/2024
8.7.6267.38147 17 05/27/2024
8.6.6263.34639 17 05/28/2024
8.5.6255.20272 19 05/27/2024
8.4.6254.24670 17 05/28/2024
8.3.6252.27823 16 05/27/2024
8.2.6249.40706 13 05/27/2024
8.1.6249.15650 16 05/28/2024
8.0.6242.20256 17 05/28/2024