【问题标题】:Fully expanding $ref references in a json schema with Python使用 Python 在 json 模式中完全扩展 $ref 引用
【发布时间】:2018-04-13 17:24:17
【问题描述】:

我们有一个相当大且复杂的 json 模式,其中包含许多通过 $ref 引用。理想情况下,使用 Python 和 jsonschema,我想采用这些模式并完全扩展引用(递归)以获得完整的模式。

dict 形式的输出很好(jsonschema 用来表示模式的标准数据结构)。

【问题讨论】:

  • 我认为不可能完全展开因为json中的$ref可以有循环

标签: python json validation schema jsonschema


【解决方案1】:

如果您检查json documentation
你会发现循环 $ref 不被推荐但不被禁止。 所以,在这种情况下,不可能完全展开所有$ref

但是如果你确定你的$ref 中没有循环,我建议你使用this repo 它在这种情况下对我有帮助。代码非常简单,您可以自行更改。

【讨论】:

    【解决方案2】:

    我已经测试过,也可以推荐以下模块:

    https://github.com/gazpachoking/jsonref

    在 PyPI 上。 documentation 很好,最近才维护(2018 年 10 月),语法是标准 json 模块的替代品:

    来自主页:

    >>> from pprint import pprint
    >>> import jsonref
    
    >>> # An example json document
    >>> json_str = """{"real": [1, 2, 3, 4], "ref": {"$ref": "#/real"}}"""
    >>> data = jsonref.loads(json_str)
    >>> pprint(data)  # Reference is not evaluated until here
    {'real': [1, 2, 3, 4], 'ref': [1, 2, 3, 4]}
    

    【讨论】:

      猜你喜欢
      • 2019-03-05
      • 2018-05-06
      • 1970-01-01
      • 2019-08-11
      • 2020-04-18
      • 2015-08-10
      • 1970-01-01
      • 1970-01-01
      • 2012-09-22
      相关资源
      最近更新 更多