Click or drag to resize
Json.NET

Create JSON Schema manually

 

This sample creates a new JsonSchema instance manually in code.

Caution note Caution

Obsolete. JSON Schema validation has been moved to its own package. See https://www.newtonsoft.com/jsonschema for more details.

Sample
Usage
JsonSchema schema = new JsonSchema();
schema.Type = JsonSchemaType.Object;
schema.Properties = new Dictionary<string, JsonSchema>
{
    { "name", new JsonSchema { Type = JsonSchemaType.String } },
    {
        "hobbies", new JsonSchema
        {
            Type = JsonSchemaType.Array,
            Items = new List<JsonSchema> { new JsonSchema { Type = JsonSchemaType.String } }
        }
    },
};

string schemaJson = schema.ToString();

Console.WriteLine(schemaJson);
// {
//   "type": "object",
//   "properties": {
//     "name": {
//       "type": "string"
//     },
//     "hobbies": {
//       "type": "array",
//       "items": {
//         "type": "string"
//       }
//     }
//   }
// }

JObject person = JObject.Parse(@"{
  'name': 'James',
  'hobbies': ['.NET', 'Blogging', 'Reading', 'Xbox', 'LOLCATS']
}");

bool valid = person.IsValid(schema);

Console.WriteLine(valid);
// true