[英]How to extract data from variable Path in JSON files using Python
[英]Extract fields and its path from json object using python
我從 elasticsearch 索引映射中獲得了 json 對象我想根據其類型對來自 json 對象的索引字段進行分組。
https://gist.github.com/akthodu/47404880d2e5b6480a881214d41feb58
長場
act.sdeactLongDescription.properties.id.type
act.properties.actstate.type
文本域:
act.properties.sdeactLongDescription.longDescription.type
當我遍歷 json.loads 給出的輸出時,我得到字符串對象。 是否有任何 json 庫來提取像 bs4 這樣的內部元素?
您可以執行遞歸函數並查找鏈的末端,如下所示:
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.