AwkwardCoder
AwkwardCoder

Reputation: 25631

Validate JSON against a schema in .NET

I know there is a proposed standard for JSON schema validation, is there an implementation in .NET?

Upvotes: 10

Views: 13161

Answers (4)

beta Joe
beta Joe

Reputation: 361

  • Json Everything and its predecesor Manatee.Json are quite good and fast.

  • NJsonSchema comfortable api however too slow for our use case (schema closing to 100kb the json in 10s of kbs); the above mentioned Manatee and json-everything have a "flag-only" validation mode which is missing here

  • Newtonsoft (Paid) i have not checked this one

Upvotes: 3

Rico Suter
Rico Suter

Reputation: 11858

A free and open-source alternative to Json.NET is NJsonSchema (JSON Schema draft 4).

Upvotes: 17

Gurunath Navale
Gurunath Navale

Reputation: 17

Add Newtonsoft's Json NuGet Package in your solution. Add below function and pass Schema and your json response in string to below function.

  public void ValidateSchema(JsonSchema JSchema, string JsonString)  {
        JsonString = JsonString.Replace("\"", "'");
        var ArrJobj = JArray.Parse(JsonString);

        foreach (JObject jo in ArrJobj)
        {
            if (!jo.IsValid(JSchema)) throw new Exception("Schems Validation failed");

        }

    }

Hope this helps

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

Json.NET has this functionality.

Upvotes: 2

Related Questions