如何提取字典键中的键

时间:2018-06-28 16:36:30

标签: arrays json python-3.x elasticsearch

我具有以下结构:

mappings": {
"go": {
"_ttl": {something goes here},
"symbol": {},
"associationType.keyword": {},
"go_genes": {},
"go_species.keyword": {},
"symbol.autocomplete": {},
"primaryId": {},
"href.keyword": {},
"name_key": {},
"symbol.sort": {},
"name_key.keyword": {},
"go_type.keyword": {},

现在,我有兴趣提取如下内容:

mappings.go.-ttl
mappings.go.symbol
mappings.go.geo_genes
.....

这是我编写的代码,但只给了我“ go”和其他位置与go相似的节点,这些未在图片中显示。我如何修改它以获得我所期望的结果,如上述:

index = "site_index_stage"
elastic_url = "http://localhost:9200"
mapping_fields_request = "_mapping/field/*"
mapping_fields_url = "/".join([elastic_url, index, mapping_fields_request])
response = requests.get(mapping_fields_url)
print(response)
data = response.content.decode()
parsed_data = json.loads(data)
for key in parsed_data[index]['mappings']:
    print(key)

1 个答案:

答案 0 :(得分:1)

这是我的做法,最后:

def nested_dict_iter(nested):
    for key, value in nested.items():
        # print((key,value))
        if type(value)==type({}):
            for x in value:
                output.write(key+'.'+x)
                output.write('\n')
nested_dict_iter(parsed_data[index]['mappings'])