I'm trying to create a method to validate a Json string with a Json schema using this method: http://www.newtonsoft.com/json/help/html/JsonSchema.htm
It says the object is obsolete and moved to its own package, so I use NuGet
and install the package (Newtonsoft.Json.dll
and Newtonsoft.Json.Schema.dll
are references) and have:
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Linq;
public bool validateSchema(string _Json)
{
JsonSchema schema = JsonSchema.Parse(
@"{
'properties': {
[MySchemaHere]
}
");
JObject jobject = JObject.Parse(_Json);
return jobject.IsValid(schema);
}
How do I get rid of the obsolete message? It sounds to me like the code has been moved to the other package/dll, but is called/used in the same way and I'm somehow referencing the obsolete one? This seems like I'm missing something simple/obvious.
EDIT: Here's an image that might help.
Yet Newtonsoft. Json was basically scrapped by Microsoft with the coming of . NET Core 3.0 in favor of its newer offering designed for better performance, System. Text.
The simplest way to check if JSON is valid is to load the JSON into a JObject or JArray and then use the IsValid(JToken, JsonSchema) method with the JSON Schema. To get validation error messages, use the IsValid(JToken, JsonSchema, IList<String> ) or Validate(JToken, JsonSchema, ValidationEventHandler) overloads.
Once we launch this application, we shall get a field called the Sample JSON Document. Here, we have to add the JSON body whose scheme we want to validate. Then click on the Generate Schema button. Then the corresponding scheme for the JSON gets generated at the bottom of the page.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, see http://www.gnu.org/licenses/agpl-3.0.html.
I finally just created a new project and copied/pasted their example and I see my painfully obvious mistake that I've been fighting with.
I should be using:
JSchema
and not
JsonSchema
My problem got resolved when I added a new NuGet Package after searching for "JSON.net schema", which resulted in showing another Newtonsoft.Json.Schema Package in option:
Tools > Nuget Package Manager > Manage Nuget Packages for Solution
After doing this, change JSONSchema object to JSchema object. This will remove the obsolete message and compile the code properly as well.
after:
string schemaJson = @"{
'description': 'A person',
'type': 'object',
'properties':
{
'name': {'type':'string'},
'hobbies': {
'type': 'array',
'items': {'type':'string'}
}
}
}";
change:
JsonSchema schema = JsonSchema.Parse(schemaJson);
to
JSchema schema = JSchema.Parse(schemaJson);
Are you sure that you have this dll? ,your problem seems to be this JSON Schema validation has been moved to its own package, check more info here:
http://www.newtonsoft.com/json/help/html/N_Newtonsoft_Json_Schema.htm
Hope this help
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With