Generate C# classes from JSON Schema

.NetWcfJsonJsonschema

.Net Problem Overview


I'm creating a C# WCF Web Service that return a lot of data in a JSON format. The client is an iPad application that is currently being developped by another team, So I'm working on specifications, without example data.
Currently the JSON string is created by the .net framework, my Web Service is returning a C# object containing all the information that are then serialized by the framework using DataContracts.

My problem is that the communication specifications only contain JSON Schema files (based on http://json-schema.org/). In order to facilitate the development I'd like to generate the corresponding classes in C# but as the files contain quite a lot of information and there are a dozen of files, I don't really want to create those classes manually.

So I'm looking for a tool that would allow me either :

  • To generate C# classes from a JSON Schema.
  • To convert a JSON Schema to an XSD file. Then it would be easy to create the classes as there are plenty of tool to generate classes from XSD.

I found a lot of tools to validate a JSON string against a JSON Schema or to generate classes from the JSON string but nothing that seem to help me.
There is JSON.NET but it seems to be a library and not a tool and I didn't found any information about generating classes with it.

So if anyone knows a tools or has an idea on how I could generate those classes (I tried a tool that create the classes in Java but I couldn't make it work).

.Net Solutions


Solution 1 - .Net

Visual Studio 2017 has this feature.

From the menu, choose Edit, Paste Special, Paste JSON As Classes. Paste in the JSON and Visual Studio will create the required classes.

enter image description here

Solution 2 - .Net

Look up this library on nuget. 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.

Solution 3 - .Net

You can use the library NJsonSchema to read a JSON schema or generate one from a type, and generate a C# class from it.

If you need a GUI for these tasks, you can try the NSwagStudio GUI from the NSwag tools to do so... (it is also based on NJsonSchema)

Solution 4 - .Net

In order to answer this correctly you need to know what version (draft) the Json Schema has.

Examples which libraries can handle which Schema (2018-01-19):

Json.NET Schema supports draft 3, draft 4, draft 6 (MIT)
Manatee.Json supports draft 4, draft 6, draft 7 (MIT)
NJsonSchema supports draft 4 (Ms-PL)

http://json-schema.org/implementations.html#validator-dotnet

With NJsonSchema.CodeGeneration you can't send the actual JSON in directly either, you first need to convert it to an actual schema (You will often get the error: Unable to cast object of type 'System.String' to type 'NJsonSchema.JsonSchema4 otherwise).

Example with running code, Schemas folder located at project root:

class Program
{
    static void Main(string[] args)
    {
        var location = Assembly.GetExecutingAssembly().Location;
        var path = Path.GetFullPath(Path.Combine(location, @"..\..\..\Schemas"));
        var schemaJson = File.ReadAllText($"{path}Test.json");
        var schema = JsonSchema4.FromJsonAsync(schemaJson).Result;
        var generator = new CSharpGenerator(schema);
        var generatedFile = generator.GenerateFile();
    }
}

Solution 5 - .Net

> So I'm looking for a tool that would allow me either : > To generate C# classes from a JSON Schema...

I haven't used it myself so I can't comment too much about it, but it seems like the tool "[json-schema-to-poco](https://github.com/codedemonuk/json-schema-to-poco/ "github/codedemonuk/json-schema-to-poco")" would fit what you need.

Per its github readme:

>Converts JSON schema files into plain old CLR objects in C#. >Useful for running as part of an automated build process.

Solution 6 - .Net

Here is an online class generator that I've used in the past to generate C# classes from a sample set of JSON data:

http://json2csharp.com/

Solution 7 - .Net

I had a need for this today and didn't see any solid answers to your question so I whipped this up. It's not perfect but it's a good starting point to build off of.

https://gist.github.com/rushfrisby/c8f58f346548bf31e045

Solution 8 - .Net

Have a look at the Help of Json.NET There is a Json.Schema namespace that can be useful.

http://james.newtonking.com/projects/json/help/

Json.NET - Quick Starts & API Documentation Newtonsoft.Json.Schema Namespace Namespaces ► Newtonsoft.Json.Schema

The project page: http://json.codeplex.com/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulien NView Question on Stackoverflow
Solution 1 - .NetSDALView Answer on Stackoverflow
Solution 2 - .NetRohit LView Answer on Stackoverflow
Solution 3 - .NetRico SuterView Answer on Stackoverflow
Solution 4 - .NetOgglasView Answer on Stackoverflow
Solution 5 - .NetTim CookeView Answer on Stackoverflow
Solution 6 - .NetBill SambroneView Answer on Stackoverflow
Solution 7 - .NetRush FrisbyView Answer on Stackoverflow
Solution 8 - .NetCarlos QuintanillaView Answer on Stackoverflow