NJsonSchema.Yaml 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.Yaml.

Packages Downloads
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
137
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
138
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
140
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
142
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
143
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
144
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
145
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
147
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
148
NSwag.Core.Yaml
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript
201
NSwag.Core.Yaml
NSwag: The Swagger API toolchain for .NET and TypeScript
142
NSwag.Core.Yaml
NSwag: The Swagger API toolchain for .NET and TypeScript
143
NSwag.Core.Yaml
NSwag: The Swagger API toolchain for .NET and TypeScript
145

.NET Framework 4.6.2

.NET 8.0

.NET Standard 2.0

Version Downloads Last updated
11.6.1 6 04/20/2026
11.6.0 7 04/08/2026
11.5.2 63 11/06/2025
11.5.1 88 10/01/2025
11.5.0 62 09/16/2025
11.4.0 117 07/31/2025
11.3.2 104 04/30/2025
11.3.1 83 04/30/2025
11.3.0 88 04/30/2025
11.2.0 90 04/05/2025
11.1.0 97 11/24/2024
11.0.2 111 07/20/2024
11.0.1 129 06/24/2024
11.0.0 120 02/13/2024
11.0.0-preview008 119 07/20/2024
11.0.0-preview007 115 07/20/2024
11.0.0-preview006 118 07/20/2024
11.0.0-preview005 106 07/20/2024
11.0.0-preview004 121 07/20/2024
11.0.0-preview003 112 07/20/2024
11.0.0-preview002 124 07/20/2024
11.0.0-preview001 126 07/20/2024
10.9.0 118 07/20/2024
10.8.0 183 02/04/2024
10.7.2 108 04/17/2024
10.7.1 112 07/20/2024
10.7.0 118 07/20/2024
10.6.10 148 02/04/2024
10.6.9 111 07/19/2024
10.6.8 117 07/20/2024
10.6.7 109 07/20/2024
10.6.6 120 07/20/2024
10.6.5 112 07/20/2024
10.6.4 127 07/20/2024
10.6.3 110 07/20/2024
10.6.2 128 07/20/2024
10.6.1 119 07/20/2024
10.6.0 110 07/20/2024
10.5.2 120 07/20/2024
10.5.1 128 07/20/2024
10.5.0 121 07/03/2024
10.4.6 126 07/20/2024
10.4.5 113 07/20/2024
10.4.4 116 07/20/2024
10.4.3 120 07/20/2024
10.4.2 119 07/20/2024
10.4.1 117 07/20/2024
10.4.0 115 07/20/2024
10.3.11 130 07/20/2024
10.3.10 108 07/20/2024
10.3.9 111 07/20/2024
10.3.8 113 07/20/2024
10.3.7 100 07/20/2024
10.3.6 117 07/20/2024
10.3.5 116 07/20/2024
10.3.4 131 07/20/2024
10.3.3 110 07/20/2024
10.3.2 112 07/20/2024
10.3.1 125 07/20/2024
10.3.0 108 07/20/2024
10.2.2 118 07/20/2024
10.2.1 117 07/20/2024
10.2.0 121 07/20/2024
10.1.26 123 07/20/2024
10.1.25 117 07/20/2024
10.1.24 130 07/20/2024
10.1.23 119 07/20/2024
10.1.22 127 07/20/2024
10.1.21 106 07/20/2024
10.1.20 109 07/20/2024
10.1.19 108 07/20/2024
10.1.18 102 07/20/2024
10.1.17 102 07/20/2024
10.1.16 112 07/20/2024
10.1.15 105 07/20/2024
10.1.14 99 07/20/2024
10.1.13 121 07/20/2024
10.1.12 108 07/20/2024
10.1.11 120 07/20/2024
10.1.10 115 07/20/2024
10.1.9 120 07/20/2024
10.1.8 107 07/20/2024
10.1.7 119 07/20/2024
10.1.6 129 07/20/2024
10.1.5 110 07/20/2024
10.1.4 108 07/20/2024
10.1.3 121 07/20/2024
10.1.2 115 07/20/2024
10.1.1 115 07/20/2024
10.1.0 125 07/20/2024
10.0.28 116 07/20/2024
10.0.27 113 07/20/2024
10.0.26 113 07/20/2024
10.0.25 104 07/20/2024
10.0.24 119 07/20/2024
10.0.23 121 07/20/2024
10.0.22 114 07/20/2024
10.0.21 122 07/20/2024
10.0.20 109 07/20/2024
10.0.19 117 07/20/2024
10.0.18 112 07/20/2024
10.0.17 109 07/20/2024
10.0.16 118 07/20/2024
10.0.15 120 07/20/2024
10.0.14 118 07/20/2024
10.0.13 112 07/20/2024
10.0.12 130 07/20/2024
10.0.11 116 07/20/2024
10.0.10 111 07/20/2024
10.0.9 105 07/20/2024
10.0.8 125 07/20/2024
10.0.7 116 07/20/2024
10.0.6 110 07/20/2024
10.0.5 117 07/20/2024
10.0.4 116 07/20/2024
10.0.3 125 07/20/2024
10.0.1 116 07/20/2024
10.0.0 129 07/17/2024
9.14.1 110 07/20/2024
9.14.0 117 07/20/2024
9.13.37 110 07/20/2024
9.13.36 148 07/20/2024
9.13.35 112 07/20/2024
9.13.34 108 07/20/2024
9.13.33 119 07/20/2024
9.13.32 122 07/20/2024
9.13.31 110 07/20/2024
9.13.30 123 07/20/2024
9.13.29 111 07/20/2024
9.13.28 116 07/20/2024
9.13.27 136 07/20/2024
9.13.26 119 07/20/2024
9.13.25 108 07/20/2024
9.13.24 115 07/20/2024
9.13.23 112 07/20/2024
9.13.22 112 07/20/2024
9.13.21 119 07/20/2024
9.13.20 111 07/20/2024
9.13.19 120 07/20/2024
9.13.18 104 07/20/2024
9.13.17 100 07/20/2024
9.13.16 119 07/20/2024
9.13.15 107 07/20/2024
9.13.14 105 07/20/2024
9.13.13 109 07/20/2024
9.13.12 121 07/20/2024
9.13.11 102 07/20/2024
9.13.10 125 07/20/2024
9.13.9 115 07/20/2024
9.13.8 121 07/20/2024
9.13.7 123 07/20/2024
9.13.5 114 07/20/2024
9.13.4 118 07/20/2024
9.13.3 114 07/20/2024
9.13.2 106 07/20/2024
9.13.1 114 07/20/2024
9.13.0 118 07/20/2024
9.12.7 106 07/20/2024
9.12.6 107 07/20/2024
9.12.5 119 07/20/2024
9.12.4 108 07/20/2024
9.12.3 115 07/20/2024
9.12.2 116 07/20/2024
9.12.1 123 07/20/2024
9.12.0 110 07/20/2024
9.11.1 123 07/20/2024
9.11.0 113 07/20/2024
9.10.75 124 07/20/2024
9.10.74 106 07/20/2024
9.10.73 121 07/20/2024
9.10.72 123 07/20/2024
9.10.71 118 07/20/2024
9.10.70 107 07/20/2024
9.10.69 130 07/20/2024
9.10.68 125 07/20/2024
9.10.67 121 07/20/2024
9.10.66 118 07/20/2024
9.10.65 114 07/20/2024
9.10.64 111 07/20/2024
9.10.63 109 07/20/2024
9.10.62 109 07/20/2024
9.10.61 126 07/20/2024
9.10.60 110 07/20/2024
9.10.59 113 07/20/2024
9.10.58 123 07/20/2024
9.10.57 112 07/20/2024
9.10.56 122 07/20/2024
9.10.55 109 07/20/2024
9.10.54 122 07/20/2024
9.10.53 115 07/20/2024
9.10.52 118 07/20/2024
9.10.51 126 07/20/2024
9.10.50 103 07/20/2024
9.10.49 124 07/20/2024
9.10.48 108 07/20/2024
9.10.47 114 07/20/2024
9.10.46 115 07/20/2024
9.10.45 120 07/20/2024
9.10.44 113 07/20/2024
9.10.43 129 07/20/2024
9.10.42 111 07/20/2024
9.10.41 109 07/20/2024
9.10.40 124 07/20/2024
9.10.39 119 07/20/2024
9.10.38 110 07/20/2024
9.10.37 108 07/20/2024
9.10.36 100 07/20/2024
9.10.35 118 07/20/2024
9.10.34 124 07/20/2024
9.10.33 125 07/20/2024
9.10.32 120 07/20/2024
9.10.31 120 07/20/2024
9.10.30 115 07/20/2024
9.10.29 122 07/20/2024
9.10.28 103 07/20/2024
9.10.27 109 07/20/2024
9.10.26 110 07/20/2024
9.10.25 125 07/20/2024