I have a json object returned from an elasticsearch query as follows:
'took': 66, 'timed_out': False, '_shards': {'total': 1, 'successful': 1, 'skipped': 0, 'failed': 0},
'hits': {'total': {'value': 371, 'relation': 'eq'}, 'max_score': None, 'hits': []}, 'aggregations': {'council_response': {'doc_count_error_upper_bound': 0, 'sum_other_doc_count': 19, 'buckets': [{'key': 'Yes', 'doc_count': 86}, {'key': 'Refused', 'doc_count': 70}, {'key': 'no status available', 'doc_count': 68}, {'key': 'No', 'doc_count': 51}, {'key': 'Publicly available', 'doc_count': 26}, {'key': 'No Reply', 'doc_count': 13}, {'key': 'No ', 'doc_count': 12}, {'key': 'No reply', 'doc_count': 10}, {'key': 'Sort of', 'doc_count': 9}, {'key': 'Some stuff', 'doc_count': 7}]}}}
I would like to extract the doc count for each of the keys, so I have Yes - doc_count_number (Yes 86) and so on.
I have narrowed it down to:
[{'key': 'Yes', 'doc_count': 86}, {'key': 'Refused', 'doc_count': 70}, {'key': 'no status available', 'doc_count': 68}, {'key': 'No', 'doc_count': 51}, {'key': 'Publicly available', 'doc_count': 26}, {'key': 'No Reply', 'doc_count': 13}, {'key': 'No ', 'doc_count': 12}, {'key': 'No reply', 'doc_count': 10}, {'key': 'Sort of', 'doc_count': 9}, {'key': 'Some stuff', 'doc_count': 7}]
with print(result['aggregations']['council_response']['buckets']) but I can get no further. I have tried
print(result['aggregations']['council_response']['buckets']['key']['Yes']['doc_count'])
which gives the error TypeError: list indices must be integers or slices, not str
How can I make the last step to get the items I need?
buckets = result['aggregations']['council_response']['buckets']
filter(lambda d: d['key'] in ['Yes', 'yes'], buckets)
print(buckets)
produces exactly the same json object as the shorter one above and if I do this print(filter(lambda d: d['key'] in ['Yes', 'yes'], buckets)) I get: <filter object at 0x7f7a380e3d90>