gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:28:38 25 4
gpt4 key购买 nike

我有这个架构,其中有 $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

最佳答案

仅需要递归来爬取文档。实际上解决 ref 可以迭代地完成。解决循环问题留给学生作为练习。

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

关于python - 如何递归访问 JSON 中的 $ref 以完成整个 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60010686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com