使用 Python 在 json 模式中完全扩展 $ref 引用
Fully expanding $ref references in a json schema with Python
我们有一个相当大且复杂的 json 架构,其中有许多通过 $ref 引用包含。理想情况下使用 Python 和 jsonschema 我想采用这些模式并完全扩展引用(递归)以获得完整的模式。
dict 形式的输出很好(jsonschema 用来表示模式的标准数据结构)。
如果勾选 json documentation.
你会发现不推荐但不禁止循环 $ref。
所以,在这种情况下,不可能 完全展开 所有 $ref
但是,如果您确定 $ref
中没有循环,我建议您使用 this repo 它在这种情况下对我有所帮助。代码很简单,大家可以自己改。
我测试过,还可以推荐以下模块:
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]}
我们有一个相当大且复杂的 json 架构,其中有许多通过 $ref 引用包含。理想情况下使用 Python 和 jsonschema 我想采用这些模式并完全扩展引用(递归)以获得完整的模式。
dict 形式的输出很好(jsonschema 用来表示模式的标准数据结构)。
如果勾选 json documentation.
你会发现不推荐但不禁止循环 $ref。
所以,在这种情况下,不可能 完全展开 所有 $ref
但是,如果您确定 $ref
中没有循环,我建议您使用 this repo 它在这种情况下对我有所帮助。代码很简单,大家可以自己改。
我测试过,还可以推荐以下模块:
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]}