NJsonSchema 11.6.0
dotnet add package NJsonSchema --version 11.6.0
NuGet\Install-Package NJsonSchema -Version 11.6.0
<PackageReference Include="NJsonSchema" Version="11.6.0" />
<PackageVersion Include="NJsonSchema" Version="11.6.0" />
<PackageReference Include="NJsonSchema" />
paket add NJsonSchema --version 11.6.0
#r "nuget: NJsonSchema, 11.6.0"
#:package NJsonSchema@11.6.0
#addin nuget:?package=NJsonSchema&version=11.6.0
#tool nuget:?package=NJsonSchema&version=11.6.0
NJsonSchema for .NET
NSwag | NJsonSchema | Apimundo | Namotion.Reflection
<img align="left" src="https://raw.githubusercontent.com/RSuter/NJsonSchema/master/assets/GitHubIcon.png">
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:
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:
- 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.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 is compatible. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.6.2
- Namotion.Reflection (>= 3.5.0)
- Newtonsoft.Json (>= 13.0.3)
- NJsonSchema.Annotations (>= 11.6.0)
- System.Text.Json (>= 9.0.9)
-
.NETStandard 2.0
- Namotion.Reflection (>= 3.5.0)
- Newtonsoft.Json (>= 13.0.3)
- NJsonSchema.Annotations (>= 11.6.0)
- System.Text.Json (>= 9.0.9)
-
net8.0
- Namotion.Reflection (>= 3.5.0)
- Newtonsoft.Json (>= 13.0.3)
- NJsonSchema.Annotations (>= 11.6.0)
NuGet packages (314)
Showing the top 5 NuGet packages that depend on NJsonSchema:
| Package | Downloads |
|---|---|
|
NSwag.Core
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript |
|
|
NSwag.Generation
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript |
|
|
NSwag.AspNetCore
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript |
|
|
NSwag.Generation.AspNetCore
NSwag: The OpenAPI/Swagger API toolchain for .NET and TypeScript |
|
|
NJsonSchema.Yaml
JSON Schema reader, generator and validator for .NET |
GitHub repositories (58)
Showing the top 20 popular GitHub repositories that depend on NJsonSchema:
| Repository | Stars |
|---|---|
|
chocolatey/choco
Chocolatey - the package manager for Windows
|
|
|
OrchardCMS/OrchardCore
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
|
|
|
RicoSuter/NSwag
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
|
|
|
kurrent-io/KurrentDB
KurrentDB is a database that's engineered for modern software applications and event-driven architectures. Its event-native design simplifies data modeling and preserves data integrity while the integrated streaming engine solves distributed messaging challenges and ensures data consistency.
|
|
|
umbraco/Umbraco-CMS
Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
|
|
|
Azure/azure-powershell
Microsoft Azure PowerShell
|
|
|
nuke-build/nuke
🏗 The AKEless Build System for C#/.NET
|
|
|
dafny-lang/dafny
Dafny is a verification-aware programming language
|
|
|
Squidex/squidex
Headless CMS and Content Managment Hub
|
|
|
phongnguyend/Practical.CleanArchitecture
Full-stack .Net 10 Clean Architecture (Microservices, Modular Monolith, Monolith), Blazor, Angular 21, React 19, Vue 3.5, BFF with YARP, NextJs 16, Domain-Driven Design, CQRS, SOLID, Asp.Net Core Identity Custom Storage, OpenID Connect, EF Core, OpenTelemetry, SignalR, Background Services, Health Checks, Rate Limiting, Clouds (Azure, AWS, GCP), ...
|
|
|
BrighterCommand/Brighter
A framework for building messaging apps with .NET and C#.
|
|
|
KSP-CKAN/CKAN
The Comprehensive Kerbal Archive Network
|
|
|
tareqimbasher/NetPad
A cross-platform C# editor and playground.
|
|
|
NethermindEth/nethermind
A robust, high-performance execution client for Ethereum node operators.
|
|
|
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
|
|
|
paillave/Etl.Net
Mass processing data with a complete ETL for .net developers
|
|
|
flyingpie/windows-terminal-quake
Enable Quake-style dropdown for (almost) any application.
|
|
|
dotnet/dotnet-monitor
This repository contains the source code for .NET Monitor - a tool that allows you to gather diagnostic data from running applications using HTTP endpoints
|
|
|
elastic/apm-agent-dotnet
|
|
|
planetarium/libplanet
Blockchain in C#/.NET for on-chain, decentralized gaming
|
| Version | Downloads | Last Updated |
|---|---|---|
| 11.6.0 | 979 | 4/8/2026 |
| 11.5.2 | 7,263,359 | 11/5/2025 |
| 11.5.1 | 1,717,518 | 9/30/2025 |
| 11.5.0 | 647,153 | 9/15/2025 |
| 11.4.0 | 3,560,995 | 7/30/2025 |
| 11.3.2 | 7,490,629 | 4/28/2025 |
| 11.3.1 | 57,154 | 4/28/2025 |
| 11.3.0 | 45,396 | 4/28/2025 |
| 11.2.0 | 2,567,405 | 3/29/2025 |
| 11.1.0 | 19,995,134 | 11/19/2024 |
| 11.0.2 | 17,974,345 | 7/17/2024 |
| 11.0.1 | 4,448,085 | 6/12/2024 |
| 11.0.0 | 20,255,673 | 1/3/2024 |
| 11.0.0-preview008 | 358,405 | 12/8/2023 |
| 11.0.0-preview007 | 4,304 | 12/8/2023 |
| 11.0.0-preview006 | 786,062 | 10/31/2023 |
| 11.0.0-preview005 | 2,694 | 10/30/2023 |
| 11.0.0-preview004 | 5,966 | 10/16/2023 |
| 11.0.0-preview003 | 36,358 | 9/26/2023 |
| 11.0.0-preview002 | 6,579 | 9/26/2023 |