[英]get list of json paths in python
I'm looking to get list of all possible json paths in a json file - can recommend any one?我正在寻找 json 文件中所有可能的 json 路径的列表 - 可以推荐任何一个吗?
Eg: if input is below例如:如果输入低于
{
"_id":{
"$oid":""
},
"aa":false,
"bb":false,
"source":"",
"email":"",
"createdAt":{
"$date":""
},
"updatedAt":{
"$date":""
},
"cc":"",
"vv":"",
"metadata":{
"vv":"",
"xx":[{}]
}
}
o/p: o/p:
obj
obj._id
obj._id.$oid
obj.aa
obj.bb
obj.source
obj.email
obj.createdAt
obj.createdAt.$date
obj.updatedAt
obj.updatedAt.$date
obj.cc
obj.vv
obj.metadata
obj.metadata.vv
obj.metadata.xx
obj.metadata.xx[0]
I'm basically looking.我基本在看。 a python version of this: https://www.convertjson.com/json-path-list.htm一个 python 版本: https://www.convertjson.com/json-path-list.htm
I want to build a general solution, if any json file - it will be a single value for schema generation (ie one line in a newline delimeted json) Any suggestions?我想构建一个通用解决方案,如果有任何 json 文件 - 它将是模式生成的单个值(即换行符分隔的 json 中的一行)有什么建议吗?
You can do this in a reasonably succinct way with a recursive generator.您可以使用递归生成器以相当简洁的方式执行此操作。 The string "obj"
is a little awkward since it doesn't occur in the data structure.字符串"obj"
有点尴尬,因为它不会出现在数据结构中。 On the other hand, adding it at the end is simple:另一方面,在最后添加它很简单:
def get_paths(d):
if isinstance(d, dict):
for key, value in d.items():
yield f'.{key}'
yield from (f'.{key}{p}' for p in get_paths(value))
elif isinstance(d, list):
for i, value in enumerate(d):
yield f'[{i}]'
yield from (f'[{i}]{p}' for p in get_paths(value))
paths = ['obj'+s for s in get_paths(d)]
Gives you paths as a list of strings:为您提供作为字符串列表的路径:
['obj._id',
'obj._id.$oid',
'obj.aa',
'obj.bb',
'obj.source',
'obj.email',
'obj.createdAt',
'obj.createdAt.$date',
'obj.updatedAt',
'obj.updatedAt.$date',
'obj.cc',
'obj.vv',
'obj.metadata',
'obj.metadata.vv',
'obj.metadata.xx',
'obj.metadata.xx[0]']
Of course, you can wrap that last step in a function like and accept a root object string:当然,您可以将最后一步包装在 function 中,并接受根 object 字符串:
def get_paths(d, root="obj"):
def recur(d):
if isinstance(d, dict):
for key, value in d.items():
yield f'.{key}'
yield from (f'.{key}{p}' for p in get_paths(value))
elif isinstance(d, list):
for i, value in enumerate(d):
yield f'[{i}]'
yield from (f'[{i}]{p}' for p in get_paths(value))
return (root + p for p in recur(d))
list(get_paths(d))
# same result
You can do this with this code:您可以使用以下代码执行此操作:
mylist = []
def getKeys(obj, parent="obj"):
global mylist
for i in obj.keys():
mylist.append(parent+"."+i)
try:
getKeys(obj[i], parent+"."+i)
except AttributeError:
pass
getKeys({
"_id":{
"$oid":""
},
"aa":False,
"bb":False,
"source":"",
"email":"",
"createdAt":{
"$date":""
},
"updatedAt":{
"$date":""
},
"cc":"",
"vv":"",
"metadata":{
"vv":"",
"xx":[{}]
}
})
print(mylist)
I changed false
to False
.我将false
更改为False
。 If you are using JSON and not a dictionary you may want to use the JSON library to convert it to a dictionary.如果您使用的是 JSON 而不是字典,您可能需要使用 JSON 库将其转换为字典。
import json
myDict = json.loads("{"_id":{"$oid":""},"aa":false,"bb":false,"source":"","email":"","createdAt":{"$date":""},"updatedAt":{"$date":""},"cc":"","vv":"","metadata":{"vv":"","xx":[{}]}}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.