Python Json引用和验证

Python Json引用和验证,json,python-3.x,jsonschema,Json,Python 3.x,Jsonschema,我开始使用python来验证一些json信息,我使用json模式进行引用,但是我很难引用这些文件。这是代码: from os.path import join, dirname from jsonschema import validate import jsonref def assert_valid_schema(data, schema_file): """ Checks whether the given data matches the schema """ sche

我开始使用python来验证一些json信息,我使用json模式进行引用,但是我很难引用这些文件。这是
代码

from os.path import join, dirname
from jsonschema import validate
import jsonref
def assert_valid_schema(data, schema_file):
    """ Checks whether the given data matches the schema """

    schema = _load_json_schema(schema_file)
    return validate(data, schema)

def _load_json_schema(filename):
    """ Loads the given schema file """

    relative_path = join('schemas', filename).replace("\\", "/")
    absolute_path = join(dirname(__file__), relative_path).replace("\\", "/")

    base_path = dirname(absolute_path)
    base_uri = 'file://{}/'.format(base_path)

    with open(absolute_path) as schema_file:
        return jsonref.loads(schema_file.read(), base_uri=base_uri, jsonschema=True, )

assert_valid_schema(data, 'grandpa.json')
json
数据是:

data = {"id":1,"work":{"id":10,"name":"Miroirs","composer":{"id":100,"name":"Maurice Ravel","functions":["Composer"]}},"recording_artists":[{"id":101,"name":"Alexandre Tharaud","functions":["Piano"]},{"id":102,"name":"Jean-Martial Golaz","functions":["Engineer","Producer"]}]}
我正在将模式和引用文件保存到一个schemas文件夹中:

recording.json:

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for a recording","type":"object","properties":{"id":{"type":"number"},"work":{"type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"composer":{"$ref":"artist.json"}}},"recording_artists":{"type":"array","items":{"$ref":"artist.json"}}},"required":["id","work","recording_artists"]}
{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for an artist","type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"functions":{"type":"array","items":{"type":"string"}}},"required":["id","name","functions"]}
artist.json:

{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for a recording","type":"object","properties":{"id":{"type":"number"},"work":{"type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"composer":{"$ref":"artist.json"}}},"recording_artists":{"type":"array","items":{"$ref":"artist.json"}}},"required":["id","work","recording_artists"]}
{"$schema":"http://json-schema.org/draft-04/schema#","title":"Schema for an artist","type":"object","properties":{"id":{"type":"number"},"name":{"type":"string"},"functions":{"type":"array","items":{"type":"string"}}},"required":["id","name","functions"]}
这是我的
错误

Connected to pydev debugger (build 181.5281.24)
Traceback (most recent call last):
  File "C:\Python\lib\site-packages\proxytypes.py", line 207, in __subject__
    return self.cache
  File "C:\Python\lib\site-packages\proxytypes.py", line 131, in __getattribute__
    return _oga(self, attr)
AttributeError: 'JsonRef' object has no attribute 'cache'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\lib\site-packages\jsonref.py", line 163, in callback
    base_doc = self.loader(uri)
<MORE>
连接到pydev调试器(build 181.5281.24)
回溯(最近一次呼叫最后一次):
主题中第207行的文件“C:\Python\lib\site packages\proxytypes.py”__
返回自缓存
文件“C:\Python\lib\site packages\proxytypes.py”,第131行,在\uu getattribute中__
返回(自身,属性)
AttributeError:'JsonRef'对象没有属性'cache'
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
回调中第163行的文件“C:\Python\lib\site packages\jsonref.py”
base_doc=self.loader(uri)
python版本:3.6.5

视窗7

Ide:intellijIdea

有人能帮我吗?
谢谢

我不知道为什么,但是在Windows上,
文件://
需要额外的
/
。因此,下面的更改应该可以做到这一点

base_uri = 'file:///{}/'.format(base_path)

从一个为相关的in-json模式发布的解决方案中得出了这个答案

我无法提供答案,因为我不熟悉这个实现,但是json模式不知道其文件位于何处,因此默认情况下,一个实现不知道如何解析这些引用!您可能只需要设置模式的
$id
,或者您可能需要使用API将模式添加到库实例中。关于如何使用该实现来实现这一点,没有太多文档可供参考:我将推动一些闲散的人来看看=]jsonref库的0.2版可能以某种奇怪的方式损坏/不兼容。尝试
pip安装jsonref==0.1
。。。