[英]How do I access the $ref in the JSON and get the 'type' in properties, given a key path in JSON?
I'm new to SO and I am really stuck on a problem I'm trying to solve at my first job.我是新来的,我真的被困在我第一份工作中试图解决的问题上。 I have this sample JSON, in which, I need to find the
"type"
of whatever key path I send it.我有这个示例 JSON,在其中,我需要找到我发送的任何关键路径的
"type"
。
For example, foo(sample_json, 'work.id') should return an "number".例如, foo(sample_json, 'work.id') 应该返回一个“数字”。
Another example, foo(sample_json, 'work.composer.artist.name') should return a "string"另一个例子, foo(sample_json, 'work.composer.artist.name') 应该返回一个“字符串”
sample_json = {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Schema for a recording",
"type": "object",
"definitions": {
"artist": {
"type": "object",
"properties": {
"id": {"type": "number"},
"name": {"type": "string"},
"functions": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["id", "name", "functions"]
}
},
"properties": {
"id": {"type": "number"},
"work": {
"type": "object",
"properties": {
"id": {"type": "number"},
"name": {"type": "string"},
"composer": {"$ref": "#/definitions/artist"}
}
},
"recording_artists": {
"type": "array",
"items": {"$ref": "#/definitions/artist"}
}
},
"required": ["id", "work", "recording_artists"]
}
I have just started writing the code.我刚开始写代码。 How do I go about it?
我该怎么做?
What would be the right way?什么是正确的方法? I know this question may not get you a lot of points, but will surely help me greatly.
我知道这个问题可能不会给你很多分,但肯定会对我有很大帮助。
def foo(schema, key_path):
key_path_list = key_path.split('.')
if key_path_list[0] in schema['properties']:
if '$ref' in schema['properties'][key_path_list[0]]:
ref = schema['properties'][key_path_list[0]]['$ref']
ref = ref.split('/')[1:]
print(ref)
#I get a list, #/definitions/artist I know I can use .get(), but how do I do recursively?
I get a list, How do I go to schema['definitions']['artist']
, then ['name']['type']
?我得到一个列表,如何转到
schema['definitions']['artist']
,然后是['name']['type']
? How do I go about making it recursive as well?我该如何让它递归?
Note, I need to manually make this function for now.请注意,我现在需要手动制作此功能。
I'm completely lost, do help me here.我完全迷失了,请在这里帮助我。 Thank you in advance.
先感谢您。
def foo(schema, key_path):
key_path_list = key_path.split('.')
#i = len(key_path_list)
#j=0
#print(key_path_list)
for item in key_path_list:
if item in schema:
#print(key_path_list)
key_path_list.remove(item)
if key_path_list:
key_path = '.'.join(key_path_list)
#print(key_path)
foo(schema[item], key_path)
else :
print(schema[item])
#print(key_path)
#print(key_path_list)
else:
if not key_path_list:
print('Please give a valid key path :)',key_path)
import json
data_ = json.loads(sample_json)
foo(data_,"properties.recording_artists.items.$ref" )
Please make sure to open json file as above请确保如上打开json文件
from functools import reduce # forward compatibility for Python 3
import operator
def foo(schema, key_path):
key_path_list = key_path.split('.')
current_dict = schema
for key in key_path_list:
if current_dict.get('properties') and current_dict['properties'].get(key):
current_dict = current_dict['properties'][key]
elif current_dict.get('$ref'):
new_path_list = current_dict['$ref'].split('/')[1:]
current_dict = reduce(operator.getitem, new_path_list, schema)
else:
print("Wrong key_path. Please provide a valid key_path")
return
return current_dict['type']
# key_path = 'work.id'
key_path = 'work.composer.artist.name'
foo(sample_json, key_path)
reduce is used here to traverse the schema.此处使用reduce 来遍历模式。 Using
sample_json
as provided above.使用上面提供的
sample_json
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.