Generate JSON Schema from C# Classes and JSON request

Generate JSON Schema from Types(C#, TS Classes) and JSON request and vice-versa

JSON Schema from JSON data or Csharp Classes net Generate JSON Schema from JSON request data or C Classes and vice versa | TheCodeBuzz

Today in this article, we will see easy techniques to generate JSON Schema from C# Classes and JSON requests.

With the recent popularity and adoption of Schema modeling over Data modeling, you may find such requirements many times where you need to generate schemas for existing databases or new databases.

As we know JSON is a very popular format for information exchange, especially when dealing with information exchanges between Microservices using RESTFul principles.

We already looked at how to generate C# classes and Typescript classes using NJSonSchema in our previous article.

Today we shall see how we can use NJsonSchema a C# library to create JSON schema files from existing JSON files or C# classes.

Using NJsonSchema Library

NJsonSchema is a .NET library to read, generate and validate JSON Schema. It provides basic code generation APIs, which can be used to generate C# and TypeScript classes or interfaces from a JSON schema.

Below are a few high-level features about this library,

One can create C# classes for any given JSON in the form of a request or type or schema file.

Getting started

Please Create any .NET Core application

  • Install NuGet package,

Install-Package NJsonSchema

OR install through Nuget Manager

JSON Schema from JSON data or Csharp Classes Generate JSON Schema from JSON request data or C Classes and vice versa | TheCodeBuzz

Let’s use the below sample data Employee.json file to generate the schema file. You could use JSON data as a string or file as required.

{
    "ID": "1002",
    "Name": "ABCD",
    "City": "city",
    "Country": "USA"
 }

Schema from JSON Data

Please use the below code to generate a schema from JSON data.

 static void Main(string[] args)
        {
            string json = File.ReadAllText("Employee.json");
            var schemaFromFile = JsonSchema.FromSampleJson(json);
            Console.WriteLine(schemaFromFile.ToJson());
         }

Generated schema as below,

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "ID": {
      "type": "string"
    },
    "Name": {
      "type": "string"
    },
    "City": {
      "type": "string"
    },
    "Country": {
      "type": "string"
    }
  }
}

Schema from C# Classes

Please use sample class Employee as below,

  public class Employee
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }

Please use the below code to generate a schema from C# type.

 static void Main(string[] args)
        {

            var schemaFromType = JsonSchema.FromType<Employee>();
            Console.WriteLine(schemaFromType.ToJson());

        }

The schema produced from the above code is as below,

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "Employee",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "ID": {
      "type": [
        "null",
        "string"
      ]
    },
    "Name": {
      "type": [
        "null",
        "string"
      ]
    },
    "City": {
      "type": [
        "null",
        "string"
      ]
    },
    "Country": {
      "type": [
        "null",
        "string"
      ]
    }
  }
}

For any customization to schema details please use JsonSchemaGeneratorSettings to define the behavior and pass it to FromSampleJson() or FromType() method(overloaded) as required.

References :

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary

Today in this article, we looked at a few easy techniques for creating JSON schema files from C# classes and JSON data. We saw how to use the NJsonSchema CodeGeneration library and with just 2-3 liner code to this libraries gives as expected output.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Comment

Your email address will not be published. Required fields are marked *