Validate JSON against a schema in .Net
-
25-09-2019 - |
Question
I know there is a proposed standard for JSON schema validation, is there an implementation in .Net?
Solution
Json.NET has this functionality.
OTHER TIPS
A free and open-source alternative to Json.NET is NJsonSchema (JSON Schema draft 4).
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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow