How to validate json schema without using Newtonsoft.Json.Schema in c# Web API

851 Views Asked by At

I have json schema and value to compare against it. I have implemented it using Newtonsoft.Json.Schema. But now realised that it is not for free use. I then tried to implement is using NJsonScehma nuget package but now getting the below error on last line of code.

cannot convert from 'Newtonsoft.Json.Linq.JToken' to 'Newtonsoft.Json.Schema.JsonSchema'

Below is my code. Any idea how to resolve this. TIA.

var jsonSchema = JObject.Parse(schemafile);  //schema definition
JToken json = JToken.Parse(value);//value to validate against schema
var result = jsonSchema.Validate(json);
2

There are 2 best solutions below

2
gregsdennis On

My validator, JsonSchema.Net, is built on System.Text.Json. It's part of a larger suite called json-everything. You can read the docs here.

I'm also one of the authors of the JSON Schema spec.

0
learningdotnet On

I have done it with NJsonSchema with below code

var json = JsonSchema.FromJsonAsync(schemafile); 
var jsonSchema = json.Result;
JToken jsonValue = JToken.Parse(value);
var result = jsonSchema.Validate(jsonValue);