Home > Back-end >  Parse Json Schema - C# - Could not resolve schema reference
Parse Json Schema - C# - Could not resolve schema reference

Time:10-02

I'm working on C# project, and I would need help to parsing of Json Schema with Newtonsoft v. 13.0.1

I aim to include in appconfig-schema.json file some json schema definitions present in Common.json file using the keyword $ref.

I have created the following json schema (filename appconfig-schema.json)

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "title": "JSON Schema for my JSON file format",
      "additionalProperties": false,
      "type": "object",
    
      "properties": {
        "proxy": {
          "type": "object",
          "properties": {
            "url": {
              "type": "string"
            },
            "port": {
              "type": "number"
            },
            "user": {
              "type": "string"
            },
            "password": {
              "type": "string"
            }
          },
          "required": [ "url", "port", "user", "password" ]
        },
    
        "ReferenceToExternalSchema": {
          "$ref": "Common.json#/definitions/ExternalType"
        }
}

And this other json schema (Common.json)

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "additionalProperties": false,
  "definitions": {
    "ExternalType": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "src": {
          "type": "string"
        }
      }
    }
  }
}

When I try to execute my code, an exception will be thrown bool validSchema = false;

        IList<string> messages;
            //JsonSchema js = JsonSchema.Parse(schemaJson);

        using (StreamReader r = new StreamReader("appconfig.json"))
        {
            string json = r.ReadToEnd();

            string schemaJson = File.ReadAllText("appconfig-schema1.json");
            JSchema schema = JSchema.Parse(schemaJson);

            items = JsonConvert.DeserializeObject<Configs>(json);
            JObject person = JObject.Parse(json);

            validSchema = person.IsValid(schema,out messages);
        }

Exception: Newtonsoft.Json.Schema.JSchemaReaderException: 'Could not resolve schema reference 'Common.json#/definitions/ExternalType'. Path 'properties.ReferenceToExternalSchema', line 27, position 34.'

please note that Visual studio 2019 recognize the Common.json file correctly enter image description here

CodePudding user response:

Interesting,

They have done it in a different way in their official documentation: https://www.newtonsoft.com/jsonschema/help/html/LoadingSchemas.htm

// person.json, has a relative external schema reference 'address.json'
// --------
// {
//   'type': 'object',
//   'properties': {
//     'name': {'type':'string'},
//     'addresses': {
//       'type': 'array',
//       'items': {'$ref': 'address.json'}
//     }
//   }
// }
// 

using (StreamReader file = File.OpenText(@"c:\person.json"))
using (JsonTextReader reader = new JsonTextReader(file))
{
    JSchemaUrlResolver resolver = new JSchemaUrlResolver();

    JSchema schema = JSchema.Load(reader, new JSchemaReaderSettings
    {
        Resolver = resolver,
        // where the schema is being loaded from
        // referenced 'address.json' schema will be loaded from disk at   
        'c:\address.json'

        BaseUri = new Uri(@"c:\person.json")
    });

    // validate JSON
}
  • Related