NJsonSchema.CodeGeneration.TypeScript 11.6.0

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
22
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
23
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
24
NSwag.Commands
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
26
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
22
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
23
NSwag.Commands
NSwag: The Swagger API toolchain for .NET and TypeScript
24

.NET Framework 4.6.2

.NET 8.0

.NET Standard 2.0

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