如何递归访问 JSON 中的$ref以完成整个 JSON?



我有这个模式,里面有$ref。我需要递归(假设没有无限循环(填写引用。也就是说,无论$ref存在何处,整个对象都会被替换为它所指向的内容。

请注意,我不能使用任何库,需要自己编写类/函数。我确实看到了jsonref,但无法理解LazyLoad和回调。

可以用更简单的方式完成吗?几天来我一直在尝试这样做。

{
"definitions": {
"pet": {
"type": "object",
"properties": {
"name":  { "type": "string" },
"breed": { "type": "string" },
"age":  { "type": "string" }
},
"required": ["name", "breed", "age"]
}
},
"type": "object",
"properties": {
"cat": { "$ref": "#/definitions/pet" },
"dog": { "$ref": "#/definitions/pet" }
}
}

成为

{
"definitions": {
"pet": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"breed": {
"type": "string"
},
"age": {
"type": "string"
}
},
"required": [
"name",
"breed",
"age"
]
}
},
"type": "object",
"properties": {
"cat": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"breed": {
"type": "string"
},
"age": {
"type": "string"
}
},
"required": [
"name",
"breed",
"age"
]
},
"dog": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"breed": {
"type": "string"
},
"age": {
"type": "string"
}
},
"required": [
"name",
"breed",
"age"
]
}
}
}

这是我正在尝试解决的 JSON,谢谢。

{
"$id": "https://example.com/nested-schema.json",
"title": "nested-schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": [
"Music",
"MusicID",
"Composer"
],
"properties": {
"MusicID": {
"type": "string",
"minLength": 0,
"maxLength": 0
},
"Music": {
"$ref": "#/definitions/Music"
},
"Composer": {
"type": "integer",
"minimum": 0,
"maximum": 0
}
},
"definitions": {
"Music": {
"type": "object",
"required": [
"Date"
],
"properties": {
"Date": {
"type": "string",
"format": "date"
},
"Artist": {
"$ref": "#/definitions/AlbumInformation"
}
}
},
"AlbumInformation": {
"type": "object",
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string",
"minLength": 5
}
}
}
},
"description": "nested-schema"
}

我们将非常感谢帮助。

我一直在尝试的一些代码:

@classmethod
def replace_refs(cls, obj, _recursive=False, **kwargs):
kwargs["_recursive"] = True
path = list(kwargs.pop("_path", ()))
if isinstance(obj, Mapping):
for k, v in obj.items():
if isinstance(v, dict):
if '$ref' in v:
import pdb
print(k, v)
cls.replace_refs(v)
return obj

递归仅用于对文档进行爬网。实际上解析引用可以迭代完成。解决周期留给学生作为练习。

import json
obj = {
"$id": "https://example.com/nested-schema.json",
"title": "nested-schema",
"$schema": "http://json-schema.org/draft-07/schema#",
"required": [
"Music",
"MusicID",
"Composer"
],
"properties": {
"MusicID": {
"type": "string",
"minLength": 0,
"maxLength": 0
},
"Music": {
"$ref": "#/definitions/Music"
},
"Composer": {
"type": "integer",
"minimum": 0,
"maximum": 0
}
},
"definitions": {
"Music": {
"type": "object",
"required": [
"Date"
],
"properties": {
"Date": {
"type": "string",
"format": "date"
},
"Artist": {
"$ref": "#/definitions/AlbumInformation"
}
}
},
"AlbumInformation": {
"type": "object",
"required": [
"Name"
],
"properties": {
"Name": {
"type": "string",
"minLength": 5
}
}
}
},
"description": "nested-schema"
}
def resolveRef(ref):
refpath = ref["$ref"].split("/")[1:]
refobj = obj
for node in refpath:
refobj = refobj[node]
resolveRefs(refobj)
return refobj
def resolveRefs(j):
for k,v in j.items():
if type(v) == dict and "$ref" in v:
j[k] = resolveRef(v)
elif type(v) == dict:
resolveRefs(j[k])
resolveRefs(obj)
print(json.dumps(obj, indent=4))

示例:https://repl.it/repls/WarmKnottySupport

最新更新