【问题标题】:Python Json reference and validationPython Json 参考和验证
【发布时间】:2018-07-11 00:19:10
【问题描述】:

我开始使用 python 来验证一些 json 信息,我正在使用带有引用的 json 模式,但我无法引用这些文件。这是code:

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"]}

艺术家.json:

{"$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"]}

这是我的error:

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>

python 版本:3.6.5

Windows 7

创意:intellijIdea

有人可以帮助我吗? 谢谢

【问题讨论】:

  • 我无法提供答案,因为我不熟悉此实现,但 JSON Schema 不知道其文件的位置,因此默认情况下实现不会知道如何解决那些参考!您可能只需要设置架构的$id,或者您可能需要使用 API 将架构添加到库实例。关于如何使用该实现执行此操作的文档不多:python-jsonschema.readthedocs.io/en/latest/references
  • 我会推一些闲人过来看看=]
  • jsonref 库的 0.2 版可能以某种奇怪的方式损坏/不兼容。试试pip install jsonref==0.1...

标签: json python-3.x jsonschema


【解决方案1】:

我不知道为什么,但在 Windows 上,file:// 需要一个额外的/。因此,以下更改应该可以解决问题

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

从针对 json 架构中的相关 issue 发布的解决方案得出此答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2010-10-18
    相关资源
    最近更新 更多