0

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>

1
  • Use Gson library from Google Commented Aug 12, 2019 at 11:23

1 Answer 1

1

Try like this:

buckets = result['aggregations']['council_response']['buckets']
list(filter(lambda d: d['key'] in ['Yes', 'yes'], buckets))[0]['doc_count']
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, it does not work. I will update my question to show you.
In what format would you like the results to be printed out? Can you update your question with the expected result?
nothing is returned with your most recent suggestion. I want to be able to do this: yes_count = result['aggregations']['council_response'] etc. no = relevant python code then I will put them into a dictionary and pass to a Flask jinja template for display in an html page.
with my latest update, I get 86 as a result, which is the count of the Yes bucket.
Thank you, maybe I am not explaining myself clearly. I would like to be able to write a line of code which goes Yes = result['aggregations']['council_response']etc. and then this variable can be passed to a dictionary which is {"Yes": yes} that I can later pass to an HTML page.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.