Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

NJsonSchema

> 编程语言
Open source

JSON Schema reader, generator and validator for .NET

1.6K stars0 likes0 views
WebsiteGitHub

About

JSON Schema reader, generator and validator for .NET

NJsonSchema for .NET

NSwag | NJsonSchema | Apimundo | Namotion.Reflection 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:

  • NJsonSchema : JSON Schema parsing, validation and generation classes
  • NJsonSchema.Annotations : JSON Schema annotations controlling serialization
  • NJsonSchema.Yaml : Read and write JSON Schemas from YAML
  • NJsonSchema.CodeGeneration : Base classes to generate code from a JSON Schema
  • NJsonSchema.CodeGeneration.CSharp : Generates CSharp classes
  • NJsonSchema.CodeGeneration.TypeScript : Generates TypeScript interfaces or classes

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

Features:

  • Read existing JSON Schemas and validate JSON data (JsonSchema.FromJsonAsync())
  • Generate JSON Schema from .NET type via reflection (with support for many attributes/annotations) (JsonSchema.FromType<MyType>())
  • Generate JSON Schema from sample JSON data (JsonSchema.FromSampleJson())
  • Support for schema references ($ref) (relative, URL and file)
  • Generate C# and TypeScript code from JSON Schema
  • Supports .NET Standard 2.0, also see XML Documentation)
  • Supports JSON Schema, Swagger and OpenAPI DTO schemas

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:

csharp
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:

…

The generated JSON schema data stored in the schemaData variable:

…

NJsonSchema.CodeGeneration usage

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

cs
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:

typescript
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:

…

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

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

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

Input:

json
{
  "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:

json
{
  "$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

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

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

Input:

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

Output:

json
{
  "$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:

  • VisualJsonEditor, a JSON schema based file editor for Windows.
  • NSwag: The Swagger API toolchain for .NET
  • SigSpec for SignalR Core: Specification and code generator for SignalR Core.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言