I got json object from elasticsearch index mapping I want to group index fields from json object based on its type.
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. Is there any json library to extract inner elements like 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.