[英]Extract fields and its path from json object using python
I got json object from elasticsearch index mapping I want to group index fields from json object based on its type.我从 elasticsearch 索引映射中获得了 json 对象我想根据其类型对来自 json 对象的索引字段进行分组。
https://gist.github.com/akthodu/47404880d2e5b6480a881214d41feb58 https://gist.github.com/akthodu/47404880d2e5b6480a881214d41feb58
long field长场
act.sdeactLongDescription.properties.id.type
act.properties.actstate.type
text field:文本域:
act.properties.sdeactLongDescription.longDescription.type
I get string object when I loop through the output given from json.loads.当我遍历 json.loads 给出的输出时,我得到字符串对象。 Is there any json library to extract inner elements like bs4?是否有任何 json 库来提取像 bs4 这样的内部元素?
You could do a recursive function and look for the end of the chain, like this:您可以执行递归函数并查找链的末端,如下所示:
import json
d = open('test.json')
test = json.load(d, strict=False)
# You could add the chain of keys to a list
def recursive_items(dictionary):
for key, value in dictionary.items():
if type(value) is dict:
# Value becomes a sub-dictionary
yield from recursive_items(value)
else:
# End of the chain
yield (key, value)
if __name__ == "__main__":
for key, value in recursive_items(test):
print(key, value)
# Regex might work too (this pattern could prob be improved a lot)
import re
pattern = r'[^,]+"type"[^,]+'
matches = re.findall(pattern, string, re.DOTALL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.