NJsonSchema.CodeGeneration.CSharp 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.CSharp.

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